1.CountDownLatch
(1) new CountDownLatch(X)
(2) countDownLatch.countDown();
一个线程执行完毕后,X减少1
(3)countDownLatch.await();
等待直至X为0
2.Java CompletableFuture:allOf等待所有异步线程任务结束
List<String> testList = Lists.newArrayList();
testList.add("cf1"); testList.add("cf2");
CompletableFuture[] cfArr = testList.stream().
map(t -> CompletableFuture
.supplyAsync(() -> pause(t), executor)
.whenComplete((result, th) -> {
System.out.println("hello" + result);
})).toArray(CompletableFuture[]::new);
// 开始等待所有任务执行完成
CompletableFuture.allOf(cfArr).join();