大家好,又见面了,我是你们的朋友全栈君。
一、线程池管理配置类
@Configuration
@EnableAsync
public class ExecutorConfig {
private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);
@Bean
public Executor asyncTaskServiceExecutor () {
logger.info("init asyncTaskServiceExecutor begin...");
/* 线程池执行管理容器 */
ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor();
//配置核心线程数
executor.setCorePoolSize(50);
//配置最大线程数
executor.setMaxPoolSize(300);
//配置队列大小
executor.setQueueCapacity(1000);
//配置线程池中的线程的名称前缀
executor.setThreadNamePrefix("async-service-");
// rejection-policy:当pool已经达到max size的时候,并且队列已经满了,如何处理新任务
// CallerRunsPolicy:不在新线程中执行任务,而是有调用者所在的线程来执行
//DiscardPolicy: 直接丢弃
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//执行初始化
executor.initialize();
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
logger.info("init asyncTaskServiceExecutor end...");
return executor;
}
}
二、VisiableThreadPoolTaskExecutor类
public class VisiableThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
private static final Logger logger = LoggerFactory.getLogger(VisiableThreadPoolTaskExecutor.class);
private void printCurrentThreadPoolInfo () {
ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
if (null == threadPoolExecutor) {
logger.info("当前异步线程池未完成初始化...");
return;
}
logger.info("当前线程池情况:名称前缀-{},任务总数-[{}],已完成的任务总数-[{}],可调度执行的工作线程总数-[{}],任务队列大小-[{}]",
this.getThreadNamePrefix(),
threadPoolExecutor.getTaskCount(),
threadPoolExecutor.getCompletedTaskCount(),
threadPoolExecutor.getActiveCount(),
threadPoolExecutor.getQueue().size()
);
}
@Override
public void execute(Runnable task) {
printCurrentThreadPoolInfo();
super.execute(task);
}
@Override
public void execute(Runnable task, long startTimeout) {
printCurrentThreadPoolInfo();
super.execute(task, startTimeout);
}
@Override
public Future<?> submit(Runnable task) {
printCurrentThreadPoolInfo();
return super.submit(task);
}
@Override
public <T> Future<T> submit(Callable<T> task) {
printCurrentThreadPoolInfo();
return super.submit(task);
}
@Override
public ListenableFuture<?> submitListenable(Runnable task) {
printCurrentThreadPoolInfo();
return super.submitListenable(task);
}
@Override
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
printCurrentThreadPoolInfo();
return super.submitListenable(task);
}
}
三、创建接口ThreadPoolAsyncTaskService
public interface ThreadPoolAsyncTaskService {
//执行异步任务
void executeAsync();
}
四、接口实现类
这里的 @Async(“asyncServiceExecutor”)中的名字asyncServiceExecutor为线程池配置管理中的方法名称;相当于是根据方法名称注入类;然后通过方法名称获取注入的类。
@Service
public class ThreadPoolAsyncTaskServiceImpl implements ThreadPoolAsyncTaskService {
Logger logger = LoggerFactory.getLogger(ThreadPoolAsyncTaskServiceImpl.class);
/**
* 异步任务退费
*/
@Override
@Async("asyncTaskServiceExecutor")
public void executeAsync() {
//做业务逻辑处理即可
logger.info("start executeAsync");
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
logger.info("end executeAsync");
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/148714.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...