java.util.concurrent.CountDownLatch类详解及例子

从名字可以看出,CountDownLatch是一个倒数计数的锁,
当倒数到0时触发事件,也就是开锁,其他人就可以进入了。
在一些应用场合中,需要等待某个条件达到要求后才能做后面的事情;同时当线程都完成后也会触发事件,以便进行后面的操作。


CountDownLatch最重要的方法是countDown()和await(),前者主要是倒数一次,后者是等待倒数到0,如果没有到达0,就只有阻塞等待了。

 

下面的例子简单的说明了CountDownLatch的使用方法,模拟了100米赛跑,10名选手已经准备就绪,只等裁判一声令下。当所有人都到达终点时,比赛结束。

 

  1. package com.eyesmore.concurrent;   
  2.   
  3. import java.util.concurrent.CountDownLatch;   
  4. import java.util.concurrent.ExecutorService;   
  5. import java.util.concurrent.Executors;   
  6.   
  7. public class CountDownLatchDemo {   
  8.   
  9.     private static final int PLAY_AMOUNT = 10;   
  10.        
  11.     public static void main(String[] args) {   
  12.   
  13.         /*
  14.           * 比赛开始:只要裁判说开始,那么所有跑步选手就可以开始跑了
  15.           * */  
  16.          CountDownLatch begin = new CountDownLatch(1);   
  17.            
  18.         /*
  19.           * 每个队员跑到末尾时,则报告一个到达,所有人员都到达时,则比赛结束
  20.           * */  
  21.          CountDownLatch end = new CountDownLatch(PLAY_AMOUNT);   
  22.          Player[] plays = new Player[PLAY_AMOUNT];   
  23.         for(int i = 0;i<PLAY_AMOUNT;i++) {   
  24.              plays[i] = new Player(i+1,begin,end);   
  25.          }   
  26.          ExecutorService exe = Executors.newFixedThreadPool(PLAY_AMOUNT);   
  27.         for(Player p : plays) {//各就各位   
  28.              exe.execute(p);   
  29.          }   
  30.          System.out.println("比赛开始");   
  31.          begin.countDown();//宣布开始   
  32.         try {   
  33.              end.await();//等待结束   
  34.          } catch (InterruptedException e) {   
  35.              e.printStackTrace();   
  36.          } finally {   
  37.              System.out.println("比赛结束");   
  38.          }   
  39.            
  40.         //注意:此时main线程已经要结束了,但是exe线程如果不关闭是不会结束的   
  41.          exe.shutdown();   
  42.      }   
  43.   
  44. }   
  45.   
  46.   
  47. class Player implements Runnable {   
  48.   
  49.     private int id;   
  50.        
  51.     private CountDownLatch begin;   
  52.        
  53.     private CountDownLatch end;   
  54.   
  55.     public Player(int id, CountDownLatch begin, CountDownLatch end) {   
  56.         super();   
  57.         this.id = id;   
  58.         this.begin = begin;   
  59.         this.end = end;   
  60.      }   
  61.   
  62.     public void run() {   
  63.         try {   
  64.              begin.await();//必须等到裁判countdown到0的时候才开始   
  65.              Thread.sleep((long)(Math.random()*100));//模拟跑步需要的时间   
  66.              System.out.println("Play "+id+" has arrived. ");   
  67.                
  68.          } catch (InterruptedException e) {   
  69.              e.printStackTrace();   
  70.          } finally {   
  71.              end.countDown();//向评委报告跑到终点了   
  72.          }   
  73.      }   
  74. }  
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值