【优化】 优化线程池,避免线程过多,造成卡顿
This commit is contained in:
@@ -28,46 +28,38 @@ public class TaskManager {
|
|||||||
|
|
||||||
/** 开始任务(每秒执行一次) */
|
/** 开始任务(每秒执行一次) */
|
||||||
public void startTask(StuInTheExam stuInTheExam, StuTheExamInfo stuTheExamInfo, String token, AtomicInteger countdown, AtomicInteger counter) {
|
public void startTask(StuInTheExam stuInTheExam, StuTheExamInfo stuTheExamInfo, String token, AtomicInteger countdown, AtomicInteger counter) {
|
||||||
// 已存在就不重复启动
|
tasks.computeIfAbsent(token, k -> {
|
||||||
tasks.computeIfAbsent(token, k ->
|
Runnable task = safe(() -> {
|
||||||
scheduler.scheduleAtFixedRate(safe(() -> {
|
|
||||||
// 每秒 + 1
|
|
||||||
int current = counter.incrementAndGet();
|
int current = counter.incrementAndGet();
|
||||||
// 获取当前值并递减
|
|
||||||
int remaining = countdown.decrementAndGet();
|
int remaining = countdown.decrementAndGet();
|
||||||
log.debug("任务 {} tick at {}", token, System.currentTimeMillis());
|
log.debug("任务 {} tick at {}", token, System.currentTimeMillis());
|
||||||
|
|
||||||
if (current == stuInTheExam.getTimes()) {
|
if (current == stuInTheExam.getTimes()) {
|
||||||
// 提示学生端进行文件的上传
|
|
||||||
stuTheExamInfo.setUpload(0);
|
stuTheExamInfo.setUpload(0);
|
||||||
// 重新进行计时
|
|
||||||
counter.set(0);
|
counter.set(0);
|
||||||
}
|
}
|
||||||
// 如果计数减到0,取消任务
|
|
||||||
if (remaining <= 0) {
|
if (remaining <= 0) {
|
||||||
ScheduledFuture<?> future = tasks.get(token);
|
ScheduledFuture<?> future = tasks.remove(token);
|
||||||
if (future != null) {
|
if (future != null) {
|
||||||
future.cancel(false);
|
future.cancel(false);
|
||||||
tasks.remove(token);
|
|
||||||
// 发送最后一条消息
|
|
||||||
stuTheExamInfo.setEndStatus(0);
|
stuTheExamInfo.setEndStatus(0);
|
||||||
log.info("任务 {} 已完成并取消", token);
|
log.info("任务 {} 已完成并取消", token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 时间转变
|
stuTheExamInfo.setTime(formatLongDuration(remaining));
|
||||||
String time = formatLongDuration(remaining);
|
|
||||||
stuTheExamInfo.setTime(time);
|
|
||||||
webSocketSenderApi.sendObject(UserTypeEnum.ADMIN.getValue(), "InTheExam", stuTheExamInfo);
|
webSocketSenderApi.sendObject(UserTypeEnum.ADMIN.getValue(), "InTheExam", stuTheExamInfo);
|
||||||
}), 0, 1, TimeUnit.SECONDS)
|
});
|
||||||
);
|
return scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
|
||||||
|
});
|
||||||
log.info("任务 {} 已启动", token);
|
log.info("任务 {} 已启动", token);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 结束任务 */
|
/** 结束任务 */
|
||||||
public void stopTask(String token) {
|
public void stopTask(String token) {
|
||||||
ScheduledFuture<?> f = tasks.remove(token);
|
ScheduledFuture<?> future = tasks.get(token);
|
||||||
if (f != null) {
|
if (future != null) {
|
||||||
f.cancel(false);
|
future.cancel(false);
|
||||||
|
tasks.remove(token);
|
||||||
log.info("任务 {} 已停止", token);
|
log.info("任务 {} 已停止", token);
|
||||||
} else {
|
} else {
|
||||||
log.warn("任务 {} 不存在", token);
|
log.warn("任务 {} 不存在", token);
|
||||||
|
Reference in New Issue
Block a user