欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

CountDownLatch同步工具类使用详解

时间:2021-06-15 09:14:58|栏目:JAVA代码|点击:

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 的工作原理。

上一篇:java实现电话本管理系统

栏    目:JAVA代码

下一篇:ant使用指南详细入门教程

本文标题:CountDownLatch同步工具类使用详解

本文地址:http://www.codeinn.net/misctech/142269.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有