import java.util.concurrent.CountDownLatch;
/**
* Created by zyBourn on 15/12/25.
*/
public class Entity {
public static void main(String[] args) {
//倒数计时 每执行一次countDown则减一次,总数减完后执行
CountDownLatch countDownLatch=new CountDownLatch(2);
new Racer(countDownLatch,"A").start();
new Racer(countDownLatch,"B").start();
new Racer(countDownLatch,"C").start();
new Racer(countDownLatch,"D").start();
for (int i=0;i<3;i++){
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(i);
countDownLatch.countDown();
if (i==2)
System.out.println("Start");
}
}
}
class Racer extends Thread{
private CountDownLatch countDownLatch;
public Racer(CountDownLatch countDownLatch,String name){
setName(name);
this.countDownLatch=countDownLatch;
}
public void run(){
try{
countDownLatch.await();
for(int i = 0;i<3;i++)
System.out.println(getName()+"=:"+i);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
CountDownLatch 计数栓
点赞
收藏