13693261870
6 天以前 73e913fb24bf163ab9c5332ab960b1eb56a6402b
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
package com.terra.system.helper;
 
 
import com.terra.common.entity.all.StaticData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
/**
 * 异步帮助类
 * @author WWW
 * @date 2023-07-10
 */
public class AsyncHelper {
    /**
     * 操作延迟10毫秒
     */
    private final static int OPERATE_DELAY_TIME = 10;
 
    private final static Log log = LogFactory.getLog(AsyncHelper.class);
 
    private ScheduledExecutorService executor = SpringContextHelper.getBean("scheduledExecutorService");
 
    public AsyncHelper() {
    }
 
    /**
     * 执行任务
     */
    public void execute(TimerTask task) {
        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }
 
    /**
     * 关闭任务
     */
    public void shutdown() {
        shutdownAndAwaitTermination(executor);
    }
 
    /**
     * 停止任务线程池
     */
    public static void shutdownAndAwaitTermination(ExecutorService pool) {
        try {
            if (null == pool || pool.isShutdown()) {
                return;
            }
 
            pool.shutdown();
            if (!pool.awaitTermination(StaticData.I120, TimeUnit.SECONDS)) {
                pool.shutdownNow();
            }
        } catch (InterruptedException ie) {
            pool.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }
}