紧急任务线程修改

master
Cui YongXing 2024-09-05 20:09:28 +08:00
parent 366a3e613e
commit aea5c583b2
2 changed files with 10 additions and 6 deletions

View File

@ -277,7 +277,7 @@ public class TaskInfoServiceImpl extends ServiceImpl<TaskInfoMapper, TaskInfo> i
if (Weight.urgency.getValue().equals(weight)){
log.info("执行紧急任务");
// 调整线程分配以适应紧急任务
adjustForEmergency();
for (long i = 1; i <= count; i++) {
long pageNum = (i - 1) * PAGE_SIZE;
@ -290,8 +290,7 @@ public class TaskInfoServiceImpl extends ServiceImpl<TaskInfoMapper, TaskInfo> i
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// 任务完成后恢复默认线程分配
restoreDefaults();
}
long end = System.currentTimeMillis();
log.info("执行时间:{}",end-start);

View File

@ -1,6 +1,7 @@
package com.muyu.task.server.thread;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class OptimizedPrioritizedThreadPool {
private static final ExecutorService executor;
@ -20,7 +21,7 @@ public class OptimizedPrioritizedThreadPool {
private static final int emergencyLowThreads = 5;
private static volatile boolean inEmergencyMode = false;
private static final AtomicInteger activeEmergencyTasks = new AtomicInteger(0);// 紧急任务计数器
static {
if (defaultHighThreads + defaultMediumThreads + defaultLowThreads > totalThreads) {
throw new IllegalArgumentException("Default priority threads exceed total threads.");
@ -42,7 +43,9 @@ public class OptimizedPrioritizedThreadPool {
}
public static void submitEmergencyTask(Runnable task) {
if (activeEmergencyTasks.incrementAndGet()==1){
adjustForEmergency();
}
try {
emergencySemaphore.acquire();
executor.submit(() -> {
@ -50,7 +53,9 @@ public class OptimizedPrioritizedThreadPool {
task.run();
} finally {
emergencySemaphore.release();
if (activeEmergencyTasks.decrementAndGet()==0){
restoreDefaults();
}
}
});
} catch (InterruptedException e) {