寻找报错
parent
0116c52990
commit
bedcd79ac2
|
@ -1,9 +1,8 @@
|
||||||
package com.muyu.task.server.thread;
|
package com.muyu.task.server.thread;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class OptimizedPrioritizedThreadPool {
|
public class OptimizedPrioritizedThreadPool {
|
||||||
|
|
||||||
private static final ExecutorService executor;
|
private static final ExecutorService executor;
|
||||||
|
@ -25,6 +24,7 @@ public class OptimizedPrioritizedThreadPool {
|
||||||
private static volatile boolean inEmergencyMode = false;
|
private static volatile boolean inEmergencyMode = false;
|
||||||
public static final AtomicInteger activeEmergencyTasks = new AtomicInteger(0); // 紧急任务计数器
|
public static final AtomicInteger activeEmergencyTasks = new AtomicInteger(0); // 紧急任务计数器
|
||||||
public static final AtomicInteger remainingTasks = new AtomicInteger(0); // 剩余任务计数器
|
public static final AtomicInteger remainingTasks = new AtomicInteger(0); // 剩余任务计数器
|
||||||
|
private static final CountDownLatch emergencyLatch = new CountDownLatch(emergencyThreads); // 用于等待所有紧急任务完成
|
||||||
|
|
||||||
static {
|
static {
|
||||||
if (defaultHighThreads + defaultMediumThreads + defaultLowThreads > totalThreads) {
|
if (defaultHighThreads + defaultMediumThreads + defaultLowThreads > totalThreads) {
|
||||||
|
@ -53,6 +53,7 @@ public class OptimizedPrioritizedThreadPool {
|
||||||
adjustForEmergency();
|
adjustForEmergency();
|
||||||
}
|
}
|
||||||
remainingTasks.incrementAndGet(); // 增加剩余任务计数
|
remainingTasks.incrementAndGet(); // 增加剩余任务计数
|
||||||
|
emergencyLatch.countDown(); // 减少计数器
|
||||||
try {
|
try {
|
||||||
emergencySemaphore.acquire();
|
emergencySemaphore.acquire();
|
||||||
executor.submit(() -> {
|
executor.submit(() -> {
|
||||||
|
@ -157,6 +158,14 @@ public class OptimizedPrioritizedThreadPool {
|
||||||
|
|
||||||
// 退出紧急模式
|
// 退出紧急模式
|
||||||
inEmergencyMode = false;
|
inEmergencyMode = false;
|
||||||
|
|
||||||
|
// 确保所有紧急任务都完成了
|
||||||
|
try {
|
||||||
|
emergencyLatch.await(); // 等待所有紧急任务完成
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
System.err.println("Interrupted while waiting for emergency tasks to complete.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue