异步实现的几种方式
程序员文章站
2022-05-13 21:22:23
...
//@Async配置,第一终实现方式,创建一个bean:
@Configuration
@EnableAsync //必要
public class ThreadTaskConfigration {
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
ThreadPoolTaskExecutor taskExecutor=new ThreadPoolTaskExecutor();
//核心线程
taskExecutor.setCorePoolSize(5);
//最大线程数
taskExecutor.setMaxPoolSize(50);
//队列最大长度 >=mainExecutor.maxSize
taskExecutor.setQueueCapacity(1000);
//线程池维护线程所允许的空闲时间
taskExecutor.setKeepAliveSeconds(60*5);
//线程池对拒绝任务(无线程可用)的处理策略
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
return taskExecutor;
}
}
@Async的第二种实现方式,继承方法。后期补上