CountDownLatch是什么饺汹?
jdk1.5開始concurrent包里提供的金句,并發(fā)編程工具類戚长。
CountDownLatch這個類能夠使一個線程等待其他線程完成各自的工作后再執(zhí)行括勺。例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動框架服務(wù)的線程已經(jīng)啟動所有的框架服務(wù)之后再執(zhí)行胞谭。
CountDownLatch如何工作?
CountDownLatch是通過一個計數(shù)器來實現(xiàn)的男杈,計數(shù)器的初始值為線程的數(shù)量丈屹。每當(dāng)一個線程完成了自己的任務(wù)后,計數(shù)器的值就會減1伶棒。當(dāng)計數(shù)器值到達(dá)0時旺垒,它表示所有的線程已經(jīng)完成了任務(wù),然后在閉鎖上等待的線程就可以恢復(fù)執(zhí)行任務(wù)苞冯。
API
countDownLatch.countDown()
countDownLatch.await();
CountDownLatch應(yīng)用場景例子
比如陪媳婦去看病袖牙。
醫(yī)院里邊排隊的人很多,如果一個人的話舅锄,要先看大夫鞭达,看完大夫再去排隊交錢取藥司忱。
現(xiàn)在我們是雙核,可以同時做這兩個事(多線程)畴蹭。
假設(shè)看大夫花3秒鐘坦仍,排隊交費取藥花5秒鐘。我們同時搞的話叨襟,5秒鐘我們就能完成繁扎,然后一起回家(回到主線程)。
代碼如下:
/**
* 看大夫任務(wù)
* Created by jiapeng on 2018/1/7.
*/
public class SeeDoctorTask implements Runnable{
private CountDownLatch countDownLatch;
public SeeDoctorTask(CountDownLatch countDownLatch){
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
Thread.sleep(3000);
System.out.println("看大夫成功糊闽,大夫給開了些藥單子");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if(countDownLatch != null) {
countDownLatch.countDown();
}
}
}
}
/**
* 排隊的任務(wù)
* Created by jiapeng on 2018/1/7.
*/
public class QueueTask implements Runnable{
private CountDownLatch countDownLatch;
public QueueTask(CountDownLatch countDownLatch){
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
Thread.sleep(5000);
System.out.println("排隊成功梳玫,可以開始交費");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if(countDownLatch != null) {
countDownLatch.countDown();
}
}
}
}
/**
* 配媳婦去看病,輪到媳婦看大夫時
* 我就開始去排隊準(zhǔn)備交錢了右犹。
* Created by jiapeng on 2018/1/7.
*/
public class MainClient {
public static void main(String[] args) throws Exception{
long now = System.currentTimeMillis();
CountDownLatch countDownLatch = new CountDownLatch(2);
Executor executor = Executors.newFixedThreadPool(2);
executor.execute(new SeeDoctorTask(countDownLatch));
executor.execute(new QueueTask(countDownLatch));
countDownLatch.await();
System.out.println("over提澎,回家 cost:"+(System.currentTimeMillis()-now));
}
}
CountDownLatch底層原理
CountDownLatch通過AQS(AbstractQueuedSynchronizer)里面的共享鎖來實現(xiàn)的。
ReentrantLock也是使用AQS