CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。

CountDownLatch如其所写,是一个倒计数的锁存器,当计数减至0时触发特定的事件。利用这种特性,可以让主线程等待子线程的结束。

主要方法

public CountDownLatch(int count);

public void countDown();

public void await() throws InterruptedException

构造方法参数指定了计数的次数

countDown方法,当前线程调用此方法,则计数减一

awaint方法,调用此方法会一直阻塞当前线程,直到计时器的值为0

ExecutorService thradPool = Executors.newFixedThreadPool(5);

final CountDownLatch cl = new CountDownLatch(5);

for(int i = 0 ; i<5; i++){

  thradPool.execute(new Runnable() {

  @Override

  public void run() {

    cl.countDown();

  }

}

thradPool.shutdown();

cl.await();  //全部线程执行完毕之后

最新文章

  1. jQuery学习之路(1)-选择器
  2. 安全的将excel导入sqlite3的解决方案
  3. JVM内存区域异常分析
  4. 德国GFZ
  5. NFine的后台源码
  6. VC++遇到的错误汇集
  7. open和fopen的区别
  8. php 日期
  9. 极化码之tal-vardy算法(3)
  10. VS2019正式版注册码秘钥
  11. Hadoop2.7.7_HA高可用部署
  12. windows下Graphviz安装及入门教程
  13. APICloud之封装webApp
  14. Windows server 2016 支持容器 ,安装docker 搭建Ubuntu+hadoop (docker为服务器)
  15. Win10还原被Windows Defender隔离的文件
  16. &#39;Table is Marked as crashed and should be repaired Error&#39;.Mysql表损坏解决方案
  17. [COGS257]动态排名系统 树状数组套主席树
  18. 以太坊go-ethereum客户端查询交易列表(二)
  19. HDU 1275 两车追及或相遇问题(相遇和追及公式)
  20. Windows下安装pillow、opencv库问题,亲测可行

热门文章

  1. 最受欢迎的5款PHP框架记录,我居然一个不知道。。。
  2. Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
  3. MySQL5.7表空间加密
  4. POJ2796 单调队列
  5. 自学了三天的SeaJs学习,解决了前端的一些问题,与小伙伴们一起分享一下!
  6. linux 服务自动调用
  7. Java [leetcode 8] String to Integer (atoi)
  8. linux下查看串口信息
  9. RTP/RTCP(一)-H264关于RTP协议的实现
  10. 【JS】&lt;select&gt;标签小结