AtomicLong total = new AtomicLong();
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setThreadNamePrefix("同步培训记录-");
threadPoolTaskExecutor.setCorePoolSize(cpuNum);
threadPoolTaskExecutor.setMaxPoolSize(cpuNum * 2);
threadPoolTaskExecutor.setQueueCapacity(1000);
threadPoolTaskExecutor.setKeepAliveSeconds(300);
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
threadPoolTaskExecutor.initialize();
Long count = trainingRecordService.count(new TrainingRecord());
while (total.get() <= count){
threadPoolTaskExecutor.execute(() -> {
if (total.get() > count){return;}
List<TrainingRecord> list = trainingRecordService.listFromHana(new TrainingRecord(), pageNum.get(), pageSize);
//查询部门信息
int size = list.size();
long before = total.getAndAdd(size);
if (total.get() > count){
list = null;
total.set(before);
return;
}
list = initDept(list);
pageNum.getAndIncrement();
trainingRecordService.batchInsert(list);
list = null;
});
}
threadPoolTaskExecutor.shutdown();
使用 Spring 内部线程池时,一定要手动自定义线程池,配置合理的参数,不然会出现生产问题(一个请求创建一个线程)。
@Configuration
@EnableAsync
public class ThreadPoolExecutorConfig {
@Bean(name="threadPoolExecutor")
public Executor threadPoolExecutor(){
ThreadPoolTaskExecutor threadPoolExecutor = new ThreadPoolTaskExecutor();
int processNum = Runtime.getRuntime().availableProcessors(); // 返回可用处理器的Java虚拟机的数量
int corePoolSize = (int) (processNum / (1 - 0.2));
int maxPoolSize = (int) (processNum / (1 - 0.5));
threadPoolExecutor.setCorePoolSize(corePoolSize); // 核心池大小
threadPoolExecutor.setMaxPoolSize(maxPoolSize); // 最大线程数
threadPoolExecutor.setQueueCapacity(maxPoolSize * 1000); // 队列程度
threadPoolExecutor.setThreadPriority(Thread.MAX_PRIORITY);
threadPoolExecutor.setDaemon(false);
threadPoolExecutor.setKeepAliveSeconds(300);// 线程空闲时间
threadPoolExecutor.setThreadNamePrefix("test-Executor-"); // 线程名字前缀
return threadPoolExecutor;
}
}