实例讲解Java并发编程之闭锁

作者:junjie 时间:2023-10-25 14:25:07 

闭锁相当于一扇门,在闭锁到达结束状态之前,这扇门一直是关闭着的,没有任何线程可以通过,当到达结束状态时,这扇门才会打开并容许所有线程通过。它可以使一个或多个线程等待一组事件发生。闭锁状态包括一个计数器,初始化为一个正式,正数表示需要等待的事件数量。countDown方法递减计数器,表示一个事件已经发生,而await方法等待计数器到达0,表示等待的事件已经发生。CountDownLatch强调的是一个线程(或多个)需要等待另外的n个线程干完某件事情之后才能继续执行。

场景应用:
10个运动员准备赛跑,他们等待裁判一声令下就开始同时跑,当最后一个人通过终点的时候,比赛结束。10个运动相当于10个线程,这里关键是控制10个线程同时跑起来,还有怎么判断最后一个线程到达终点。可以用2个闭锁,第一个闭锁用来控制10个线程等待裁判的命令,第二个闭锁控制比赛结束。


import java.util.concurrent.CountDownLatch;

class Aworker implements Runnable {
private int num;
private CountDownLatch begin;
private CountDownLatch end;

public Aworker(int num, final CountDownLatch begin, final CountDownLatch end) {
this.num = num;
this.begin = begin;
this.end = end;
}

@Override
public void run() {
// TODO Auto-generated method stub
try {
 System.out.println(num + "th people is ready");
 begin.await();  //准备就绪
} catch (InterruptedException e) {
 e.printStackTrace();
} finally {
 end.countDown();  //计数器减一,到达终点
 System.out.println(num + "th people arrive");
}
}
}

public class Race {
public static void main(String[] args) {
int num = 10;
CountDownLatch begin = new CountDownLatch(1);
CountDownLatch end = new CountDownLatch(num);

for (int i = 1; i <= num; i++) {
 new Thread(new Aworker(i, begin, end)).start();
}

try {
 Thread.sleep((long) (Math.random() * 5000));
} catch (InterruptedException e1) {
 // TODO Auto-generated catch block
 e1.printStackTrace();
}
System.out.println("judge say : run !");
begin.countDown(); //裁判一声令下开始跑
long startTime = System.nanoTime();
try {
 end.await(); //等待结束
} catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
} finally {
 long endTime = System.nanoTime();
 System.out.println("judge say : all arrived !");
 System.out.println("spend time: " + (endTime - startTime));
}
}
}

输出


1th people is ready
2th people is ready
4th people is ready
6th people is ready
3th people is ready
10th people is ready
8th people is ready
5th people is ready
7th people is ready
9th people is ready
judge say : run !
1th people arrive
4th people arrive
10th people arrive
5th people arrive
2th people arrive
judge say : all arrived !
9th people arrive
7th people arrive
8th people arrive
3th people arrive
6th people arrive
spend time: 970933
标签:Java,并发编程,闭锁
0
投稿

猜你喜欢

  • C# Linq延迟查询的执行实例代码

    2023-04-24 05:34:59
  • Java 获取网站图片的示例代码

    2022-03-11 14:52:44
  • android系统在静音模式下关闭camera拍照声音的方法

    2021-09-04 00:34:20
  • SpringBoot之使用Redis实现分布式锁(秒杀系统)

    2022-09-30 15:45:08
  • Java实体类不要使用基本类型的知识点总结

    2023-02-21 10:04:49
  • 简单好用的Adapter---ArrayAdapter详解

    2022-09-08 08:33:05
  • Java超详细讲解类变量和类方法

    2023-04-24 14:45:43
  • C++双向循环列表用法实例

    2023-07-23 03:58:54
  • C#简单的加密类实例

    2022-12-15 11:31:18
  • SpringBoot整合mybatis-plus进阶详细教程

    2023-11-27 05:13:12
  • jpa实现多对多的属性时查询的两种方法

    2022-01-14 00:47:45
  • Java线程池源码的深度解析

    2023-10-02 19:33:40
  • java微信公众号开发(搭建本地测试环境)

    2021-07-07 05:32:40
  • Android Studio项目适配AndroidX(Android 9.0)的方法步骤

    2022-10-30 12:01:03
  • springboot返回图片流的实现示例

    2023-11-23 17:30:08
  • JAVA Map架构和API介绍

    2023-01-25 14:01:38
  • Android Splash界面白屏、黑屏问题的解决方法

    2022-03-24 07:14:44
  • 浅谈java中对集合对象list的几种循环访问

    2022-02-11 02:32:33
  • SpringBoot Knife4j在线API文档框架基本使用

    2022-03-10 21:27:48
  • Android编程之canvas绘制各种图形(点,直线,弧,圆,椭圆,文字,矩形,多边形,曲线,圆角矩形)

    2023-10-16 12:16:14
  • asp之家 软件编程 m.aspxhome.com