返回顶部

多个线程都完成才执行主线程

public static void test1() throws InterruptedException {
        //初始化线程数量
        CountDownLatch countDownLatch=ThreadUtil.newCountDownLatch(5);
        for(int i=0;i<5;i++){
            ThreadUtil.execute(() -> {
                try {
                    Thread.sleep(6000);
                    System.out.println("111" );
                    //调用线程计数器-1
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        //唤醒主线程
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("完成");

    }

 

posted @ 2022-11-10 17:40  不懒人  阅读(104)  评论(0编辑  收藏  举报