本篇聊聊同步輔助類CountDownLatch,涉及內容基于JDK7。
1.概述
CountDownLatch允許一個或者多個線程一直等待,直到一組其它操作執行完成。在使用CountDownLatch時,需要指定一個整數值,此值是線程將要等待的操作數。當某個線程為了要執行這些操作而等待時,需要調用await方法。await方法讓線程進入休眠狀態直到所有等待的操作完成為止。當等待的某個操作執行完成,它使用countDown方法來減少CountDownLatch類的內部計數器。當內部計數器遞減為0時,CountDownLatch會喚醒所有調用await方法而休眠的線程們。
2.使用樣例
下面代碼演示了CountDownLatch簡單使用。演示的場景是5位運動員參加跑步比賽,發令槍打響后,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.創構造方法
CountDownLatch(int count)構造一個指定計數的CountDownLatch,count為線程將要等待的操作數。
4.常用方法
4.1 await()
調用await方法后,使當前線程在鎖存器(內部計數器)倒計數至零之前一直等待,進入休眠狀態,除非線程被中斷。如果當前計數遞減為零,則此方法立即返回,繼續執行。
4.2 await(long timeout, TimeUnit unit)
調用await方法后,使當前線程在鎖存器(內部計數器)倒計數至零之前一直等待,進入休眠狀態,除非線程被 中斷或超出了指定的等待時間。如果當前計數為零,則此方法立刻返回true值。
4.3 acountDown()
acountDown方法遞減鎖存器的計數,如果計數到達零,則釋放所有等待的線程。如果當前計數大于零,則將計數減少。如果新的計數為零,出于線程調度目的,將重新啟用所有的等待線程。
4.4 getCount()
調用此方法后,返回當前計數,即還未完成的操作數,此方法通常用于調試和測試。