欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CountDownLatch同步工具类使用详解

程序员文章站 2023-08-13 20:44:27
countdownlatch 使用说明,供大家参考,具体内容如下 countdownlatch是一种java.util.concurrent包下一个同步工具类,它允许一个...

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同步工具类使用详解

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

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

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

CountDownLatch同步工具类使用详解

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

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

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

CountDownLatch同步工具类使用详解

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

我们猜测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();
    }
    
  }

运行一下,下面是结果

CountDownLatch同步工具类使用详解

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

这样看来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 的工作原理。

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。