CountDownLatch 使用说明

CountDownLatch是一种java.util.concurrent包下一个同步工具类,它允许一个或多个线程等待直到在其他线程中一组操作执行完成。

CountDownLatch的用法非常简单,下面的例子也是我在网上看到的,十分贴切,这里就贴出来

public class Test {

    public static void main(String[] args) {
       CountDownLatch begin = new CountDownLatch(1);
       CountDownLatch end = new CountDownLatch(2);

       for(int i=0; i<2; i++){
           Thread thread = new Thread(new Player(begin,end));
           thread.start();
       }

       try{
           System.out.println("the race begin");
           begin.countDown();
           end.await();
           System.out.println("the race end");
       }catch(Exception e){
            e.printStackTrace();
       }

    }
}


/**
 * 选手
 */
class Player implements Runnable{

    private CountDownLatch begin;

    private CountDownLatch end;

    Player(CountDownLatch begin,CountDownLatch end){
        this.begin = begin;
        this.end = end;
    }

    public void run() {
        
        try {
            begin.await();
            System.out.println(Thread.currentThread().getName() + " arrived !");;
            end.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

下面是运行结果

可以看到 通过CountDownLatch 的使用  我们控制了线程的执行顺序

在上面代码中,我们使用到await()方法  和  countDown()  方法 。我们验证一下它们各自的作用。

首先  验证await() 方法。将main方法中的 end.await() 注释掉,下面是注释掉后的运行结果

 

可以看到主线程没有等待代表选手的线程结束,直接宣布比赛结束了!刚开始就结束的比赛- -

这里可以看出,await() 方法具有阻塞作用

 

其次  我们来验证countDown方法,将代表选手线程中的 end.countDown() 进行注释,下面是运行结果

 

程序一直在运行,所有选手都已经到了终点,但是裁判就是不宣传比赛结束,他在等什么呢?

我们猜测countDown() 方法具有唤醒阻塞线程的作用。

那我们也许会问,既然有唤醒阻塞线程的作用,那么我们只调用一次countDown()  方法不就是可以唤醒被阻塞的主线程了吗?

我们试一下,取消上面coutDown()的注释,再次创建一个选手,代码如下

  class Player2 implements Runnable{

        private CountDownLatch begin;

        private CountDownLatch end;

        Player2(CountDownLatch begin,CountDownLatch end){
            this.begin = begin;
            this.end = end;
        }

        public void run() {
            
            try {
                begin.await();
                System.out.println(Thread.currentThread().getName() + " arrived !");
//                end.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

 

main 方法也修改如下,创建了两个不同的选手

public static void main(String[] args)
    {
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(2);
        
        Thread thread = new Thread(new Player(begin, end));
        thread.start();
        
        Thread thread2 = new Thread(new Player2(begin, end));
        thread2.start();
        
        try
        {
            System.out.println("the race begin");
            begin.countDown();
            end.await();
            System.out.println("the race end");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
    }

运行一下,下面是结果

主程序一直阻塞,没有被唤醒,裁判上厕所上得有点久啊!

这样看来countDown() 并不是直接唤醒线程,有点像一个计数器,倒计时的那种。

查看API文档,果然,我们在构造函数中添加了参数2,就需要调用 2 次 countDown()  才能将 end.await() 阻塞的线程唤醒。

CountDownLatch end = new CountDownLatch(2);

 

总结一下,

  1、CountDownLatch end = new CountDownLatch(N); //构造对象时候 需要传入参数N

  2、end.await()  能够阻塞线程 直到调用N次end.countDown() 方法才释放线程

  3、end.countDown() 可以在多个线程中调用  计算调用次数是所有线程调用次数的总和

 

下一篇博客  我将从源码层面说明 CountDownLatch 的工作原理。

 

posted on 2018-03-15 10:49  猫咪大王  阅读(39694)  评论(5编辑  收藏  举报