CountDownLatch同步工具类使用详解

2025-05-29 0 29

countdownlatch 使用说明,供大家参考,具体内容如下

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50
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()的注释,再次创建一个选手,代码如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
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 方法也修改如下,创建了两个不同的选手

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
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() 阻塞的线程唤醒。

?

1
countdownlatch end = new countdownlatch(2);

总结一下,

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

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

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

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

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

原文链接:http://www.cnblogs.com/cuglkb/p/8572239.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 CountDownLatch同步工具类使用详解 https://www.kuaiidc.com/112177.html

相关文章

发表评论
暂无评论