自定义异步任务线程池
@Async 注解 方法将使用该线程池
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(25);
executor.setMaxPoolSize(50);
executor.setQueueCapacity(1000);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("ASYNC-TASK-THREAD-");
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setRejectedExecutionHandler(
new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
自定义调度任务线程池
@Scheduled 注解方法将使用该线程池
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(getSchedulerExecutor());
}
public Executor getSchedulerExecutor() {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(25,
new ThreadFactoryBuilder().setNameFormat("BUSI-SCHEDULER-%d").build());
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}