CountDownLatch线程计数器的使用

new CountDownLatch(3); 构造的时候可以传入一个整数(其实是long),记录次数
latch.await() 会阻塞线程,直到latch的count为0,才执行后面的逻辑
latch.countDown() 会减1

下面用春游的例子来说明用法,代码如下:

/**
 * 这里用春游的例子来举例
 * 例如3个人一起去春游,路上的时间不同,要都集合了才能去春游
 */
public class CountDownLatchDemo implements Runnable{
    private CountDownLatch latch;
    public CountDownLatchDemo(CountDownLatch latch){
        this.latch = latch;
    }
    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName()+"已出发");
            Thread.sleep((1+(int)(Math.random()*(5-1)))*1000); // 路途中的时间 1到5秒
            latch.countDown();
            System.out.println(Thread.currentThread().getName()+"已到达");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        CountDownLatch latch = new CountDownLatch(3);
        System.out.println(Thread.currentThread().getName()+"起床啦,今天要春游");
        for (int i=0;i<3;i++){
            new Thread(new CountDownLatchDemo(latch),"同学"+i).start();
        }
        try {
            latch.await();
            System.out.println(Thread.currentThread().getName()+":大家都到达了,走起");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

发布了422 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/enthan809882/article/details/104189210