在一些應用場合中,某段程序需要等待某個條件達到要求後才能執行,或者等待一定長的時間後此行,從jdk1.5開始就可以使用CountDownLatch實現,
CountDownLatch類是一個同步倒數計數器,構造時傳入int參數,該參數就是計數器的初始值,每調用一次countDown()方法,計數器減1,計數器大於0 時,await()方法會阻塞後面程序執行,直到計數器為0,await(long timeout, TimeUnit unit),是等待一定時間,然後執行,不管計數器是否到0了。
下面舉一個等車的例子:
10個同學上車,車等待同學上車,如果有等待時間限制,到時間就開走,不管學生上沒上完。如果沒有等待時間,學生上完了再開: public class CountDownLatchTest {
public static int numberOfPeople = 10;//等車的學生數
public static boolean isGone = false;//車開的標志
public static int carWaitTime = 3;//車等的時間
public static void main(String[] args) throws InterruptedException {
CountDownLatch waitStudentsGetOn = new CountDownLatch(numberOfPeople);
new Thread(new GetOn(waitStudentsGetOn)).start();
waitStudentGetOn(waitStudentsGetOn);//等所有的學生上車
driveHome();//開車走
}
private static void waitStudentGetOn(CountDownLatch waitStudentsGetOn) throws InterruptedException {
System.out.println("趕緊的,抓緊時間上車..");
waitStudentsGetOn.await(carWaitTime, TimeUnit.SECONDS);//等5秒,還沒上車,就開走。。
}
private static void driveHome() throws InterruptedException {
System.out.println("開車,鞋兒破 帽兒破 身上的袈裟破 你笑我 他笑我 一把扇兒破");
isGone = true;
}
}
class GetOn implements Runnable{
private CountDownLatch waitStudentsGetOn;
GetOn(CountDownLatch waitStudentsGetOn){
this.waitStudentsGetOn = waitStudentsGetOn;
}
public void run() {
for (int i = 0; i < CountDownLatchTest.numberOfPeople; i++) {
try {
if(CountDownLatchTest.isGone){
System.out.println("媽的,還差:"+waitStudentsGetOn.getCount()+" 個沒娃上車呢.怎麼車走了");
break;
}
boolean goonSuccess = new Student(i+1).getOn();//順序上車
if(goonSuccess)waitStudentsGetOn.countDown();
} catch (InterruptedException e) {}
if(waitStudentsGetOn.getCount()!=0l){
System.out.println("還差:"+(waitStudentsGetOn.getCount())+" 個沒上車");
}else{
System.out.println("都上車了");
}
}
}
class Student{
private int myNum;//學生編號
public Student(int num){
this.myNum = num;
}
//上車
public boolean getOn() throws InterruptedException{
Thread.currentThread().sleep(new Random().nextInt(2)*1000);//上車使用的時間,隨機
if(CountDownLatchTest.isGone){
return false;//不能上了,上車失敗
}
System.out.print("編號為:"+myNum+"的同學上車了..");
return true;
}
}
}