闭锁
    是一个一次性产品,只要满足了count > 0 那么主线程的都就不会阻塞

    1. ExecutorService executorService = Executors.newFixedThreadPool(10);
    2. /*
    3. 业务场景为主线程要等待其他线程全部都执行完成才进行下面接下来的处理
    4. */
    5. CountDownLatch downLatch = new CountDownLatch(10);
    6. for (int i = 0; i < 10; i++) {
    7. executorService.execute(() -> {
    8. try {
    9. System.out.println(Thread.currentThread().getName() + "执行中。。。");
    10. Thread.sleep(5000L);
    11. } catch (InterruptedException e) {
    12. e.printStackTrace();
    13. }finally {
    14. System.out.println(Thread.currentThread().getName() + "执行完成释放一个计数");
    15. downLatch.countDown();
    16. }
    17. });
    18. }
    19. downLatch.await();
    20. System.out.println("线程池执行完成,主线程继续执行");