本篇聊聊同步輔助類CountDownLatch,涉及內容基于JDK7法焰。
1.概述
CountDownLatch允許一個或者多個線程一直等待,直到一組其它操作執(zhí)行完成埃仪。在使用CountDownLatch時,需要指定一個整數(shù)值卵蛉,此值是線程將要等待的操作數(shù)。當某個線程為了要執(zhí)行這些操作而等待時傻丝,需要調用await方法。await方法讓線程進入休眠狀態(tài)直到所有等待的操作完成為止葡缰。當?shù)却哪硞€操作執(zhí)行完成,它使用countDown方法來減少CountDownLatch類的內部計數(shù)器运准。當內部計數(shù)器遞減為0時缭受,CountDownLatch會喚醒所有調用await方法而休眠的線程們。
2.使用樣例
下面代碼演示了CountDownLatch簡單使用米者。演示的場景是5位運動員參加跑步比賽,發(fā)令槍打響后蔓搞,5個計時器開始分別計時,直到所有運動員都到達終點喂分。
public class CountDownLatchDemo {
public static void main(String[] args) {
Timer timer = new Timer(5);
new Thread(timer).start();
for (int athleteNo = 0; athleteNo < 5; athleteNo++) {
new Thread(new Athlete(timer, "athlete" + athleteNo)).start();
}
}
}
class Timer implements Runnable {
CountDownLatch timerController;
public Timer(int numOfAthlete) {
this.timerController = new CountDownLatch(numOfAthlete);
}
public void recordResult(String athleteName) {
System.out.println(athleteName + " has arrived");
timerController.countDown();
System.out.println("There are " + timerController.getCount() + " athletes did not reach the end");
}
@Override
public void run() {
try {
System.out.println("Start...");
timerController.await();
System.out.println("All the athletes have arrived");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Athlete implements Runnable {
Timer timer;
String athleteName;
public Athlete(Timer timer, String athleteName) {
this.timer = timer;
this.athleteName = athleteName;
}
@Override
public void run() {
try {
System.out.println(athleteName + " start running");
long duration = (long) (Math.random() * 10);
Thread.sleep(duration * 1000);
timer.recordResult(athleteName);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
輸出結果如下所示:
Start...
athlete0 start running
athlete1 start running
athlete2 start running
athlete3 start running
athlete4 start running
athlete0 has arrived
There are 4 athletes did not reach the end
athlete3 has arrived
There are 3 athletes did not reach the end
athlete2 has arrived
athlete1 has arrived
There are 1 athletes did not reach the end
There are 2 athletes did not reach the end
athlete4 has arrived
There are 0 athletes did not reach the end
All the athletes have arrived
3.創(chuàng)構造方法
CountDownLatch(int count)構造一個指定計數(shù)的CountDownLatch蒲祈,count為線程將要等待的操作數(shù)。
4.常用方法
4.1 await()
調用await方法后梆掸,使當前線程在鎖存器(內部計數(shù)器)倒計數(shù)至零之前一直等待,進入休眠狀態(tài)酸钦,除非線程被中斷。如果當前計數(shù)遞減為零,則此方法立即返回徒恋,繼續(xù)執(zhí)行。
4.2 await(long timeout, TimeUnit unit)
調用await方法后入挣,使當前線程在鎖存器(內部計數(shù)器)倒計數(shù)至零之前一直等待,進入休眠狀態(tài)财岔,除非線程被 中斷或超出了指定的等待時間。如果當前計數(shù)為零匠璧,則此方法立刻返回true值。
4.3 acountDown()
acountDown方法遞減鎖存器的計數(shù)魔眨,如果計數(shù)到達零,則釋放所有等待的線程遏暴。如果當前計數(shù)大于零,則將計數(shù)減少朋凉。如果新的計數(shù)為零,出于線程調度目的杂彭,將重新啟用所有的等待線程。
4.4 getCount()
調用此方法后亲怠,返回當前計數(shù),即還未完成的操作數(shù)团秽,此方法通常用于調試和測試。