天天看點

多個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();
           

繼續閱讀