package com.lf.server.helper;
|
|
|
import com.lf.server.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();
|
}
|
}
|
}
|