SpringBoot默认异步任务配置
默认情况下,Spring 将搜索关联的线程池定义:上下文中唯一的org.springframework.core.task.TaskExecutor bean,或者名为“taskExecutor”的java.util.concurrent.Executor bean。 如果两者都不可解析,则将使用org.springframework.core.task.SimpleAsyncTaskExecutor来处理异步方法调用。
/**
* 默认异步任务配置
*
* @author Tan
* @version 1.0
* @date 2021/6/2
*/
@EnableAsync(proxyTargetClass = true)
@Configuration
public class DefaultAsyncTaskConfiguration {
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心池大小是保持活动状态的最小工作线程数
executor.setCorePoolSize(4);
// 最大线程数
executor.setMaxPoolSize(64);
// 线程最大空闲时间/秒(根据使用情况指定)
executor.setKeepAliveSeconds(60);
// 任务队列大小(根据使用情况指定)
executor.setQueueCapacity(128);
// 线程名称前缀
executor.setThreadNamePrefix("BaseExecutor-");
/*
rejectedExecutionHandler: 当pool线程数达到maxPoolSize时, 如何处理被拒线程任务
ThreadPoolExecutor.CallerRunsPolicy: 不在新线程中执行任务,而是有调用者所在的线程来执行
*/
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}
@Async
会使用taskExecutor
线程池配置