闭锁
是一个一次性产品,只要满足了count > 0 那么主线程的都就不会阻塞
ExecutorService executorService = Executors.newFixedThreadPool(10);
/*
业务场景为主线程要等待其他线程全部都执行完成才进行下面接下来的处理
*/
CountDownLatch downLatch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
executorService.execute(() -> {
try {
System.out.println(Thread.currentThread().getName() + "执行中。。。");
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println(Thread.currentThread().getName() + "执行完成释放一个计数");
downLatch.countDown();
}
});
}
downLatch.await();
System.out.println("线程池执行完成,主线程继续执行");