UserInfo currentUser = this.getCurrentUser();
if (CollectionUtils.isNotEmpty(examCategoryFeeList)) {
//用并行流处理插入数据基础字段,如果不处理,会包空指针
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(100);
threadPoolTaskExecutor.setMaxPoolSize(100);
threadPoolTaskExecutor.setKeepAliveSeconds(3000);
threadPoolTaskExecutor.setQueueCapacity(500);
// 线程池需要初始化!
threadPoolTaskExecutor.initialize();
int nThreads = 100;
int size = examCategoryFeeList.size();
//每个线程执行的插入行数,如果不加1,会造成数据丢失:1537/100=15就会丢失数据,所以是(15+1)*100
int threadSize = examCategoryFeeList.size() / nThreads + 1;
List<Future<Integer>> futures = new ArrayList<>(nThreads);
// 100个线程
for (int i = 0; i < nThreads; i++) {
//判断线程中分配到需要处理的数据 是否小于总数
if (threadSize * (i + 1) <= size) {
//如果小于总数,直接截取,如果大于总数,会截取报错
final List<ExamCategoryFee> insertList = examCategoryFeeList.subList(threadSize * i, threadSize * (i + 1));
if (CollectionUtils.isNotEmpty(insertList)) {
Callable<Integer> callable = () -> {
examCategoryFeeSrv.saveAll(insertList);
return 1;
};
Future<Integer> submit = threadPoolTaskExecutor.submit(callable);
futures.add(submit);
}
} else {
//如果大于总数,截取到末尾值=size
final List<ExamCategoryFee> insertList = examCategoryFeeList.subList(threadSize * i, size);
if (CollectionUtils.isNotEmpty(insertList)) {
Callable<Integer> callable = () -> {
examCategoryFeeSrv.saveAll(insertList);
return 1;
};
Future<Integer> submit = threadPoolTaskExecutor.submit(callable);
futures.add(submit);
}
break;
}
}
if (CollectionUtils.isNotEmpty(futures)) {
try {
//确保所有线程执行所返回值,也就是所有线程执行结束
for (Future<Integer> future : futures) {
this.logInfo("保存收费归类:future", JSON.toJSONString(future));
Integer integer = future.get();
if (1 != integer) {
this.logError("保存收费归类失败");
}
}
threadPoolTaskExecutor.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
return true;
线程池实现批量插入
最新推荐文章于 2024-07-14 03:21:19 发布