admin
2024-04-24 363457b34e0e4f26ffe51aa80ecb227bf7873308
提交 | 用户 | 时间
363457 1 package com.jcdm.framework.manager;
A 2
3 import java.util.TimerTask;
4 import java.util.concurrent.ScheduledExecutorService;
5 import java.util.concurrent.TimeUnit;
6 import com.jcdm.common.utils.Threads;
7 import com.jcdm.common.utils.spring.SpringUtils;
8
9 /**
10  * 异步任务管理器
11  * 
12  * @author jc
13  */
14 public class AsyncManager
15 {
16     /**
17      * 操作延迟10毫秒
18      */
19     private final int OPERATE_DELAY_TIME = 10;
20
21     /**
22      * 异步操作任务调度线程池
23      */
24     private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");
25
26     /**
27      * 单例模式
28      */
29     private AsyncManager(){}
30
31     private static AsyncManager me = new AsyncManager();
32
33     public static AsyncManager me()
34     {
35         return me;
36     }
37
38     /**
39      * 执行任务
40      * 
41      * @param task 任务
42      */
43     public void execute(TimerTask task)
44     {
45         executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
46     }
47
48     /**
49      * 停止任务线程池
50      */
51     public void shutdown()
52     {
53         Threads.shutdownAndAwaitTermination(executor);
54     }
55 }