JAVA中CountDownLatch的简单示例

 

 

 

public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch =new CountDownLatch(10);

        for (int i = 0; i < 900; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() );
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        latch.countDown();
                    }
                }
            }).start();
        }
           

        System.out.println("等待子线程运行结束");
        latch.await();
        System.out.println("子线程运行结束");

    }

 

posted @ 2021-09-02 10:17  yvioo  阅读(56)  评论(0编辑  收藏  举报