Java的并发控制中,有个很有用的类叫CountDownLatch,直译就是倒数锁,构造时传入一个初始值,其他线程都可以控制计数器-1,当计数减至0时触发特定的事件。
demo是最好的老师,下面贴上代码,看看用他如何模拟一个运动会的跑步比赛吧。
运动员类:
import java.util.concurrent.CountDownLatch;
public class Player implements Runnable {
private int id;//运动员编号
private CountDownLatch begin;//开始锁
private CountDownLatch end;//结束锁
public Player(int id,CountDownLatch begin,CountDownLatch end) {
this.id=id;
this.begin=begin;
this.end=end;
}
@Override
public void run() {
try {
begin.await();//等待比赛开始
long time=System.currentTimeMillis();
Thread.sleep((long) (Math.random()*5000));//模拟具体一个运动员跑全程花费时间,最长是5秒
System.out.println("运动员 "+id+" 到达,用时 "+(System.currentTimeMillis()-time));
} catch (Exception e) {
System.out.println(e.getMessage());
}finally{
end.countDown();//该运动员到达终点,通知latch将计数器减一
}
}
}
然后是主类:
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class LatchDemo {
public static void main(String[] args) throws InterruptedException {
Player[] players=new Player[10];//10个运动员参赛
CountDownLatch begin=new CountDownLatch(1);//比赛开始的计数锁,由比赛裁判控制
CountDownLatch end=new CountDownLatch(players.length);//比赛结束的计数锁,所有运动员跑完后停止
for(int i=0;i<players.length;i++){//为每个运动员编号,并安排到同一个比赛中(公用一样的开始与结束计数锁)
players[i]=new Player(i, begin, end);
}
ExecutorService executorService=Executors.newFixedThreadPool(players.length);
for(Player p:players){
executorService.execute(p);
}//各个运动员都准备就绪了(线程都执行起来了,但这些线程都进入等待开始的CountDownLatch中)
System.out.println("比赛开始~");
long time=System.currentTimeMillis();
begin.countDown();//裁判员吹哨,让所有的运动员都开始跑了
end.await();//裁判等待所有运动员跑完
System.out.println("比赛结束,用时"+(System.currentTimeMillis()-time));
}
}
下面是运行结果:
比赛开始~
运动员 3 到达,用时 297
运动员 9 到达,用时 336
运动员 8 到达,用时 535
运动员 0 到达,用时 1061
运动员 2 到达,用时 1464
运动员 5 到达,用时 2128
运动员 4 到达,用时 2160
运动员 1 到达,用时 2883
运动员 7 到达,用时 4461
运动员 6 到达,用时 4959
比赛结束,用时4959