项目中经常会遇到线程池异步处理一些任务
1.配置信息
# 异步线程配置 # 核心线程数 async: executor: thread: core_pool_size: 10 # 最大线程数 max_pool_size: 100 # 任务队列大小 queue_capacity: 20 # 线程池中线程的名称前缀 name: prefix: kc-async-service- # 缓冲队列中线程的空闲时间 keep_alive_seconds: 100 await_termination_seconds: 60
2.公共配置类:
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; @Configuration @EnableAsync @ComponentScan("com.gary.kc") public class AsyncConfig implements AsyncConfigurer { @Value("${async.executor.thread.core_pool_size}") private int corePoolSize; @Value("${async.executor.thread.max_pool_size}") private int maxPoolSize; @Value("${async.executor.thread.queue_capacity}") private int queueCapacity; @Value("${async.executor.thread.name.prefix}") private String namePrefix; @Value("${async.executor.thread.keep_alive_seconds}") private int keepAliveSeconds; @Value("${async.executor.thread.await_termination_seconds}") private int awaitTerminationSeconds; @Override @Bean("AsyncExecutor") public Executor getAsyncExecutor() { ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor(); //返回可用处理器的Java虚拟机的数量,可以根据数量设定线程参数,我这里直接写死了 int i = Runtime.getRuntime().availableProcessors(); //设置核心线程数 threadPool.setCorePoolSize(corePoolSize); //设置最大线程数 threadPool.setMaxPoolSize(maxPoolSize); //线程池所使用的缓冲队列 threadPool.setQueueCapacity(queueCapacity); //等待任务在关机时完成--表明等待所有线程执行完 threadPool.setWaitForTasksToCompleteOnShutdown(true); // 等待时间 (默认为0,此时立即停止),并没等待xx秒后强制停止 threadPool.setAwaitTerminationSeconds(awaitTerminationSeconds); // 线程名称前缀 threadPool.setThreadNamePrefix(namePrefix); // 线程的空闲时间 threadPool.setKeepAliveSeconds(keepAliveSeconds); // 拒绝策略 threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化线程 threadPool.initialize(); return threadPool; } }
PS:线程池拒绝策略
rejectedExectutionHandler参数字段用于配置绝策略,常用拒绝策略如下
AbortPolicy:用于被拒绝任务的处理程序,它将抛出RejectedExecutionException
CallerRunsPolicy:用于被拒绝任务的处理程序,它直接在execute方法的调用线程中运行被拒绝的任务。
DiscardOldestPolicy:用于被拒绝任务的处理程序,它放弃最旧的未处理请求,然后重试execute。
DiscardPolicy:用于被拒绝任务的处理程序,默认情况下它将丢弃被拒绝的任务。
3.使用线程池
@Async("AsyncExecutor") public void asyncBatchInsertCallList() { try { log.warn("start Batch Insert");} catch(Exception e){
log.error("start Batch Insert error:{}",e);
}
4.总结
多尝试,换个思路可能会更好。