CountDownLatch类

  允许一个或多个线程等待直到在其他线程中执行的一组操作完成的同步辅助。

  CountDownLatch能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行。使用一个计数器进行实现。计数器初始值为线程的数量。当每一个线程完成自己任务后,计数器的值就会减一。当计数器的值为0时,表示所有的线程都已经完成了任务,然后在CountDownLatch上等待的线程就可以恢复执行任务。

构造方法:

  CountDownLatch(int count)

构造一个以给定计数 CountDownLatch CountDownLatch。

方法:

  • await() 导致当前线程等到锁存器计数到零,除非线程是 interrupted 。
  • await(long timeout, TimeUnit unit) 使当前线程等待直到锁存器计数到零为止,除非线程为 interrupted或指定的等待时间过去。
  • countDown() 减少锁存器的计数,如果计数达到零,释放所有等待的线程。
  • getCount() 返回当前计数。
模拟运动员赛跑DEMO
 import concurrenttest.countdownlatch.thread.Thread_1;

 import java.util.concurrent.CountDownLatch;

 /**
* 描述:模拟运动员赛跑DEMO
*
* @author bc
* @create 2018-09-28 10:49
*/
public class RunTest_1 { public static void main(String[] args) {
try {
CountDownLatch comingTag = new CountDownLatch(10);//裁判等待运动员到来
CountDownLatch waitTag = new CountDownLatch(1);//等待裁判说准备开始
CountDownLatch waitRunTag = new CountDownLatch(10);//等待起跑
CountDownLatch beginTag = new CountDownLatch(1);//起跑
CountDownLatch endTag = new CountDownLatch(10);//到达终点 Thread_1[] arr = new Thread_1[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Thread_1(comingTag, waitTag, waitRunTag, beginTag, endTag);
arr[i].start();
} System.out.println("裁判等待运动员到来");
comingTag.await();
System.out.println("裁判看到所有运动员都来了,巡视一下");
Thread.sleep(5000);
waitTag.countDown();
System.out.println("各就各位!");
waitRunTag.await();
Thread.sleep(2000);
System.out.println("发令枪响!");
beginTag.countDown();
endTag.await();
System.out.println("所有运动员到了,统计结果");
} catch (InterruptedException e) {
e.printStackTrace();
} }
}

RunTest_1

 import java.util.concurrent.CountDownLatch;

 /**
* 描述: 模拟运动员赛跑
*
* @author bc
* @create 2018-09-28 10:10
*/
public class Thread_1 extends Thread { private CountDownLatch comingTag;//裁判等待运动员到来
private CountDownLatch waitTag;//等待裁判说准备开始
private CountDownLatch waitRunTag;//等待起跑
private CountDownLatch beginTag;//起跑
private CountDownLatch endTag;//到达终点 public Thread_1(CountDownLatch comingTag, CountDownLatch waitTag, CountDownLatch waitRunTag,
CountDownLatch beginTag, CountDownLatch endTag) {
super();
this.comingTag = comingTag;
this.waitTag = waitTag;
this.waitRunTag = waitRunTag;
this.beginTag = beginTag;
this.endTag = endTag;
} @Override
public void run() {
try {
System.out.println("运动员正飞奔而来");
Thread.sleep((int)(Math.random()*10000));
System.out.println(Thread.currentThread().getName()+"到起跑点了");
comingTag.countDown();
System.out.println("等待裁判说准备");
waitTag.await();
System.out.println("各就各位!准备起跑的姿势");
Thread.sleep((int)(Math.random()*10000));
waitRunTag.countDown();
beginTag.await();
System.out.println(Thread.currentThread().getName()+"疯狂奔跑中");
Thread.sleep((int)(Math.random()*10000));
endTag.countDown();
System.out.println(Thread.currentThread().getName()+"到达终点");
} catch (InterruptedException e) {
e.printStackTrace();
} }
}

Thread_1

CountDownLatch的不足
CountDownLatch是一次性的,计数器的值只能在构造方法中初始化一次,之后没有任何机制再次对其设置值,当CountDownLatch使用完毕后,它不能再次被使用。

详见本人github:https://github.com/BrokenColor/java-demo 下的 countdownlatch-包中的测试

最新文章

  1. 注解的方式搭建springmvc步骤
  2. JDK和环境配置
  3. Android菜鸟成长记3-activity类
  4. Visual Studio Online Integrations-Productivity
  5. Linux内核中的GPIO系统之(3):pin controller driver代码分析
  6. cocos2dx从入门到精通课程
  7. Cayley n顶点树数定理
  8. HW6.18
  9. ng-selected 与ng-options的使用
  10. MySQL之数据的简单查询
  11. ●BZOJ 4822 [Cqoi2017]老C的任务
  12. 鸟哥私房菜--进程SELinux
  13. 从面试连跪到收割offer,回顾我的春招面试历程(研发岗位)
  14. ODBC 驱动程序管理器 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配 解决方案
  15. intelliJ idea快捷方式
  16. android事务队列处理的实现
  17. 更改Eclipse下Tomcat的部署目录 ,防止上传的文件是到eclipse的克隆的tomcat上的webapp,而不是tomcat本身的webapp
  18. HDU - 4804 Campus Design 轮廓线dp
  19. 学号20155308 2016-2017-2 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
  20. redis 3.0 集群__数据迁移和伸缩容

热门文章

  1. PhotoSwipe中文API(二)
  2. rewrite or internal redirection cycle while processing &quot;/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/jenkins/
  3. R语言基本语法
  4. C++中定义NULL的头文件
  5. Specifications查询
  6. 3vim编辑器
  7. Java开发之JDK配置
  8. [POI2006][luogu3435] OKR-Periods of Words [kmp+next数组]
  9. stm32 Flash读写[库函数]
  10. 解决方案--duilib中edit获得鼠标焦点后右边框被覆盖