AsyncManager.java
1.33 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.bs.web.manager;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.bs.common.utils.Threads;
import com.bs.common.utils.spring.SpringUtils;
/**
* 异步任务管理器
*
* @author system
*/
public class AsyncManager
{
/**
* 操作延迟10毫秒
*/
private final int OPERATE_DELAY_TIME = 10;
/**
* 异步操作任务调度线程池
*/
private ScheduledExecutorService executor;
/**
* 单例模式
*/
private AsyncManager(){}
private static AsyncManager me = new AsyncManager();
public static AsyncManager me()
{
return me;
}
/**
* 获取 ScheduledExecutorService(延迟获取,避免 Spring 容器未初始化完成)
*/
private ScheduledExecutorService getExecutor() {
if (executor == null) {
executor = SpringUtils.getBean("scheduledExecutorService");
}
return executor;
}
/**
* 执行任务
*
* @param task 任务
*/
public void execute(TimerTask task)
{
getExecutor().schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
}
/**
* 停止任务线程池
*/
public void shutdown()
{
Threads.shutdownAndAwaitTermination(executor);
}
}