天天看点

多个CompletableFuture异步的执行,主线程等待所有任务完成.

代码实现:

List<String> line = new ArrayList();
List<CompletableFuture<>> totalLines = lines.stream()
                .map(productLine -> CompletableFuture.supplyAsync(() -> 
                {
        //do something async
        		}))
        .collect(Collectors.toList());
        
        //线程聚合等待        
        CompletableFuture.allOf(totalLines.toArray(new CompletableFuture[0])).join();
           

继续阅读