个人备忘
/**
* 全局线程池配置
*
* @date 2022-05-30 10:39:48
*/
@EnableAsync
@Configuration
public class ThreadPoolConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
// 核心线程数,当池内线程数小于此值时会优先创建新线程
threadPoolTaskExecutor.setCorePoolSize(4);
// 线程超时关闭时间
threadPoolTaskExecutor.setKeepAliveSeconds(60);
// 允许核心线程超时关闭
threadPoolTaskExecutor.setAllowCoreThreadTimeOut(true);
// 任务等待队列,若当前核心线程数已满则将任务放入此队列进行等待,若此队列也满则开启新线程直至达到MaxPoolSize
threadPoolTaskExecutor.setQueueCapacity(16);
// 最大线程数,若全部线程数超过此值则执行拒绝策略
threadPoolTaskExecutor.setMaxPoolSize(100);
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new AsyncUncaughtException();
}
}