CountDownLatch

计数器,指定计数器的大小,当执行latch.countDown()后计数器减一,当计数器等于0时才能执行latch.await()之后的语句。

public class Study06 {

    public static void main(String[] args) {

        Thread[] threads =new Thread[100];

        CountDownLatch latch =new CountDownLatch(threads.length);

        for (int i =0; i < threads.length; i++) {

            threads[i] =new Thread(()->{

                int result =0;

                for (int j=0; j<10000; j++){

                    result += j;

                }

                    System.out.println(Thread.currentThread().getName()+result);

                    latch.countDown();

            });

        }

        for (int i=0; i

            threads[i].start();

        }

        try {

              latch.await();

        } catch (InterruptedException e) {

                e.printStackTrace();

        }

        System.out.println("end latch");

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容