heheng
6 天以前 46ecbba9e60627c48881f9d91dff168ff25a4e32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.gkhy.exam.framework.manager;
 
import cn.hutool.extra.spring.SpringUtil;
import com.gkhy.exam.common.utils.Threads;
 
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
/**
 * 异步任务管理器
 */
public class AsyncManager {
    /**
     * 操作延迟10ms
     */
    private final int OPERATE_DELAY_TIME=10;
    /**
     * 异步操作任务调度线程池
     */
    private ScheduledExecutorService executorService= SpringUtil.getBean("scheduledExecutorService");
 
    /**
     * 单例模式
     */
    private AsyncManager(){}
 
    private static AsyncManager me=new AsyncManager();
 
    public static AsyncManager me(){
        return me;
    }
 
    public void execute(TimerTask task){
        executorService.schedule(task,OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }
 
    public void shutdown(){
        Threads.shutdownAndAwaitTermination(executorService);
    }
}