ExecutorService executor = Executors.newFixedThreadPool(5);
List<CompletableFuture<Map<String, Double>>> list=new ArrayList<>();
partitionList.stream().forEach(batchList -> {
batchList.stream().forEach(params -> {
CompletableFuture<Map<String, Double>> mapCompletableFuture = CompletableFuture.supplyAsync(() -> doPredictMeshResult(coefficient, params, param),executor)
.whenComplete((data, throwable) -> {
resultList.add(data);
paramList.add(params);
});
list.add(mapCompletableFuture);
});
});
// 所有子线程执行完,主线程才执行
CompletableFuture.allOf(list.toArray(new CompletableFuture[list.size()])).join();
实现主线程在子线程执行完后执行
于 2023-08-16 18:03:07 首次发布