多线程执行并获取返回数据
@Autowired
Executor localBootAsyncExecutor;
long start = System.currentTimeMillis();
List<cust> futures = new ArrayList();
for (custdto : datePageList){
CompletableFuture<List<cust>> future = CompletableFuture.supplyAsync(() -> {
List<cust> list = custMapper.queryCust(dto);
return list;
},localBootAsyncExecutor);
futures.add(future);
}
//待所有线程返回数据
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();
long end = System.currentTimeMillis();
log.info("============耗时(秒)==============:"+(end - start));
//汇总每个线程返回数据
for(CompletableFuture future : futures){
List<cust> list = (List<cust>) future.get();
resultList.addAll(list);
}
不需要返回值多线程
@Autowired
Executor localBootAsyncExecutor;
List<CompletableFuture> futures = new ArrayList();
futures.add(CompletableFuture.runAsync(() -> {
dataList().forEach(a -> {
RemitFlagDTO dtp = new RemitFlagDTO();
dtp.setTaskId(a.getInteger("taskId"));
dtp.setTaskCallStatus(a.getInteger("taskCallStatus"));
plmTaskListMapper.updateTaskCallStatus(dtp);
});
}, localBootAsyncExecutor));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();