闭锁相当于一扇门,在闭锁到达结束状态之前,这扇门一直是关闭着的,没有不论什么线程能够通过,当到达结束状态时。这扇门才会打开并容许全部线程通过。它能够使一个或多个线程等待一组事件发生。
闭锁状态包含一个计数器。初始化为一个正式,正数表示须要等待的事件数量。
countDown方法递减计数器。表示一个事件已经发生,而await方法等待计数器到达0。表示等待的事件已经发生。
CountDownLatch强调的是一个线程(或多个)须要等待另外的n个线程干完某件事情之后才干继续运行。
场景应用:
10个运动员准备赛跑,他们等待裁判一声令下就開始同一时候跑,当最后一个人通过终点的时候,比赛结束。
10个运动相当于10个线程,这里关键是控制10个线程同一时候跑起来,还有怎么推断最后一个线程到达终点。能够用2个闭锁,第一个闭锁用来控制10个线程等待裁判的命令,第二个闭锁控制比赛结束。
package concurrent.thread;
import java.util.concurrent.CountDownLatch;
class Racer implements Runnable{
private CountDownLatch start;
private CountDownLatch finish;
private int number;
public Racer(int number, CountDownLatch start, CountDownLatch finish){
this.start = start;
this.finish = finish;
this.number = number;
}
@Override
public void run() {
try {
System.out.println(number + "th racer ready!");
//等待起跑的命令
//注意这里不是用wait()
start.await();
System.out.println(number + "th racer finish!");
//跑完
finish.countDown();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class CountDownLatchExample {
public static void main(String[] args) throws InterruptedException{
final int num = 10;
CountDownLatch start = new CountDownLatch(1);//这里最好用CyclicBarrier
CountDownLatch finish = new CountDownLatch(num);
for(int i = 1; i <= num; i++){
new Thread(new Racer(i, start, finish)).start();;
}
//等待一秒。防止线程没准备好就開始运行
//假设用CyclicBarrier。这里就不用等待了
Thread.sleep(1000);
System.out.println("比赛開始");
//比赛開始
start.countDown();
//等待比赛结束
finish.await();
System.out.println("比赛结束");
}
}
输出:
1th racer ready!
5th racer ready!
4th racer ready!
3th racer ready!
2th racer ready!
9th racer ready!
8th racer ready!
6th racer ready!
10th racer ready!
7th racer ready!
比赛開始
1th racer finish!
3th racer finish!
6th racer finish!
8th racer finish!
2th racer finish!
9th racer finish!
4th racer finish!
5th racer finish!
7th racer finish!
10th racer finish!
比赛结束
闭锁相当于一扇门,在闭锁到达结束状态之前,这扇门一直是关闭着的,没有不论什么线程能够通过,当到达结束状态时。这扇门才会打开并容许全部线程通过。它能够使一个或多个线程等待一组事件发生。
闭锁状态包含一个计数器。初始化为一个正式,正数表示须要等待的事件数量。
countDown方法递减计数器。表示一个事件已经发生,而await方法等待计数器到达0。表示等待的事件已经发生。
CountDownLatch强调的是一个线程(或多个)须要等待另外的n个线程干完某件事情之后才干继续运行。
场景应用:
10个运动员准备赛跑,他们等待裁判一声令下就開始同一时候跑,当最后一个人通过终点的时候,比赛结束。
10个运动相当于10个线程,这里关键是控制10个线程同一时候跑起来,还有怎么推断最后一个线程到达终点。能够用2个闭锁,第一个闭锁用来控制10个线程等待裁判的命令,第二个闭锁控制比赛结束。
package concurrent.thread;
import java.util.concurrent.CountDownLatch;
class Racer implements Runnable{
private CountDownLatch start;
private CountDownLatch finish;
private int number;
public Racer(int number, CountDownLatch start, CountDownLatch finish){
this.start = start;
this.finish = finish;
this.number = number;
}
@Override
public void run() {
try {
System.out.println(number + "th racer ready!");
//等待起跑的命令
//注意这里不是用wait()
start.await();
System.out.println(number + "th racer finish!");
//跑完
finish.countDown();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class CountDownLatchExample {
public static void main(String[] args) throws InterruptedException{
final int num = 10;
CountDownLatch start = new CountDownLatch(1);//这里最好用CyclicBarrier
CountDownLatch finish = new CountDownLatch(num);
for(int i = 1; i <= num; i++){
new Thread(new Racer(i, start, finish)).start();;
}
//等待一秒。防止线程没准备好就開始运行
//假设用CyclicBarrier。这里就不用等待了
Thread.sleep(1000);
System.out.println("比赛開始");
//比赛開始
start.countDown();
//等待比赛结束
finish.await();
System.out.println("比赛结束");
}
}
输出:
1th racer ready!
5th racer ready!
4th racer ready!
3th racer ready!
2th racer ready!
9th racer ready!
8th racer ready!
6th racer ready!
10th racer ready!
7th racer ready!
比赛開始
1th racer finish!
3th racer finish!
6th racer finish!
8th racer finish!
2th racer finish!
9th racer finish!
4th racer finish!
5th racer finish!
7th racer finish!
10th racer finish!
比赛结束