|
@@ -15,6 +15,7 @@ import java.util.concurrent.*;
|
|
|
**/
|
|
|
public class ThreadPoolTaskTool {
|
|
|
public static Map<String, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();//全局唯一
|
|
|
+ public static Map<String, ScheduledFuture<?>> scheduledHistoryTasks = new ConcurrentHashMap<>();//全局唯一
|
|
|
|
|
|
|
|
|
//TODO 创建任务池
|
|
@@ -34,6 +35,28 @@ public class ThreadPoolTaskTool {
|
|
|
}
|
|
|
public static void scheduleHistroyTask(ScheduledExecutorService scheduler,String taskId,ThreadHistoryTask threadTask,Long initialDelay,Integer collectionFrequency) {
|
|
|
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(threadTask, initialDelay, collectionFrequency, TimeUnit.SECONDS);
|
|
|
- scheduledTasks.put(taskId, future);
|
|
|
+ scheduledHistoryTasks.put(taskId, future);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void cancelTask() {
|
|
|
+ for(String taskIdKey :scheduledTasks.keySet()){
|
|
|
+ ScheduledFuture<?> future = scheduledTasks.get(taskIdKey);
|
|
|
+ if (future != null) {
|
|
|
+ future.cancel(true);
|
|
|
+ scheduledTasks.remove(taskIdKey);
|
|
|
+ System.out.println("Task " + taskIdKey + " cancelled.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void cancelHistoryTask() {
|
|
|
+ for(String taskIdKey :scheduledHistoryTasks.keySet()){
|
|
|
+ ScheduledFuture<?> future = scheduledHistoryTasks.get(taskIdKey);
|
|
|
+ if (future != null) {
|
|
|
+ future.cancel(true);
|
|
|
+ scheduledHistoryTasks.remove(taskIdKey);
|
|
|
+ System.out.println("Task " + taskIdKey + " cancelled.");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|