理解
CountDownLatch是用來(lái)協(xié)調(diào)多個(gè)線(xiàn)程之間的通信,它能夠使一個(gè)線(xiàn)程需要等待另外一個(gè)線(xiàn)程執(zhí)行完后杜窄,然后在繼續(xù)執(zhí)行莉钙。其中是使用了計(jì)數(shù)器來(lái)實(shí)現(xiàn)的蚊伞,根據(jù)初始值來(lái)設(shè)置需要等待的線(xiàn)程數(shù)量,
當(dāng)這些線(xiàn)程都執(zhí)行完后,也就是初始值為0時(shí)预吆,在CountDownLatch上等待的線(xiàn)程就能繼續(xù)執(zhí)行。
源碼解析
我們主要來(lái)看看內(nèi)部的一個(gè)結(jié)構(gòu)和主要的方法await()和countDown()
首先是有個(gè)內(nèi)部類(lèi)Sync繼承了AbstractQueuedSynchronizer
private static final class Sync extends AbstractQueuedSynchronizer {
private static final long serialVersionUID = 4982264981922014374L;
Sync(int count) {
//設(shè)置計(jì)數(shù)器的初始值
setState(count);
}
int getCount() {
//返回當(dāng)前的計(jì)數(shù)器值
return getState();
}
//判斷等待的線(xiàn)程是否能夠執(zhí)行了返吻,為0時(shí)表示可以執(zhí)行
protected int tryAcquireShared(int acquires) {
//當(dāng)前計(jì)數(shù)器為0時(shí)表示等待的線(xiàn)程可以執(zhí)行了
return (getState() == 0) ? 1 : -1;
}
//當(dāng)其他線(xiàn)程執(zhí)行完后計(jì)數(shù)器減1
protected boolean tryReleaseShared(int releases) {
// Decrement count; signal when transition to zero
for (;;) {
int c = getState();
// 如果當(dāng)前計(jì)數(shù)器已經(jīng)為0了,說(shuō)明有其他線(xiàn)程已經(jīng)執(zhí)行完畢,等待的線(xiàn)程已經(jīng)
//在執(zhí)行了吊档,直接返回false
if (c == 0)
return false;
int nextc = c-1;
//嘗試更新計(jì)算器的值
if (compareAndSetState(c, nextc))
return nextc == 0;
}
}
}
在來(lái)看看構(gòu)造方法,內(nèi)部只有一個(gè)構(gòu)造方法 ,就是設(shè)置計(jì)算數(shù)的初始值
public CountDownLatch(int count) {
if (count < 0) throw new IllegalArgumentException("count < 0");
this.sync = new Sync(count);
}
然后是await方法 進(jìn)行了重載
//調(diào)用這個(gè)方法時(shí)就會(huì)被掛起蹋岩,直到計(jì)數(shù)器為0時(shí)就繼續(xù)執(zhí)行
public void await() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
//這個(gè)和上面的區(qū)別就是設(shè)置了時(shí)間阀坏,等待一段時(shí)間后計(jì)數(shù)器還沒(méi)為0時(shí)就繼續(xù)執(zhí)行
public boolean await(long timeout, TimeUnit unit)
throws InterruptedException {
return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
}
最后是countDown方法
//每調(diào)用一次計(jì)數(shù)器的值減1,直到0
public void countDown() {
sync.releaseShared(1);
}
下面我們來(lái)看看使用例子
public class CountDown {
public static void main(String[] args) {
CountDownLatch downLatch=new CountDownLatch(2);
try {
new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"開(kāi)始執(zhí)行:"+LocalTime.now());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"執(zhí)行完畢:"+LocalTime.now());
downLatch.countDown();
}
}.start();
new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"開(kāi)始執(zhí)行:"+LocalTime.now());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"執(zhí)行完畢:"+LocalTime.now());
downLatch.countDown();
}
}.start();
downLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"執(zhí)行完畢蕊爵。");
}
}
//執(zhí)行結(jié)果
Thread-1開(kāi)始執(zhí)行:16:40:47.426
Thread-0開(kāi)始執(zhí)行:16:40:47.426
Thread-1執(zhí)行完畢:16:40:49.426
Thread-0執(zhí)行完畢:16:40:49.426
main執(zhí)行完畢。
總結(jié)
CountDownLatch是用于某個(gè)線(xiàn)程等待若干個(gè)線(xiàn)程執(zhí)行完畢后先慷,它才執(zhí)行的媒役。缺點(diǎn)就是CountDownLatch是不能夠重用的席爽,只能用一次,如果需要重用咬腋,可以考慮用CyclicBarrier妻怎。