CountDownLatch
概念
countDownLatch這個(gè)類(lèi)使一個(gè)線程等待其他線程各自執(zhí)行完畢后再執(zhí)行庸汗。
是通過(guò)一個(gè)計(jì)數(shù)器來(lái)實(shí)現(xiàn)的柠座,計(jì)數(shù)器的初始值是線程的數(shù)量迄损。每當(dāng)一個(gè)線程執(zhí)行完畢后省有,計(jì)數(shù)器的值就-1,當(dāng)計(jì)數(shù)器的值為0時(shí)贯涎,表示所有線程都執(zhí)行完畢听哭,然后在閉鎖上等待的線程就可以恢復(fù)工作了。
核心方法
//構(gòu)造方法,初始化一個(gè)CountDownLatch實(shí)例,指定計(jì)數(shù)count,一般指定為多線程的數(shù)量
public CountDownLatch(int count);
//阻塞線程,等待所有線程執(zhí)行完成釋放,繼續(xù)執(zhí)行后續(xù)代碼
public void await() throws InterruptedException;
//阻塞線程,指定阻塞時(shí)間,阻塞時(shí)間超過(guò)后,無(wú)論所有線程是否執(zhí)行完畢,都將繼續(xù)執(zhí)行后續(xù)代碼
public boolean await(long timeout, TimeUnit unit) throws InterruptedException;
//計(jì)數(shù)器減1
public void countDown();
//獲取當(dāng)前計(jì)數(shù)器的值
public long getCount();
示例
不指定阻塞時(shí)間
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author scottxuan
*/
@Slf4j
public class CountDownLatchExample1 {
private final static int threadCount = 20;
public static void main(String[] args) throws InterruptedException {
//初始化計(jì)數(shù)器,一般和線程數(shù)目相同
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < threadCount; i++) {
final int num = i;
executor.execute(()->{
count(num);
//多線程每執(zhí)行一次,計(jì)數(shù)器減1
countDownLatch.countDown();
});
}
//主線程阻塞,等待所有線程執(zhí)行完畢,才會(huì)執(zhí)行后續(xù)的代碼
countDownLatch.await();
log.info("all thread finish");
executor.shutdown();
}
private static void count(int num){
log.info("current num {}",num);
}
}
//執(zhí)行結(jié)果,所有線程執(zhí)行完畢后,日志輸出 all thread finish
//22:17:56.095 [pool-1-thread-1] INFO com.aqs.CountDownLatchExample1 - current num 0
//22:17:56.095 [pool-1-thread-9] INFO com.aqs.CountDownLatchExample1 - current num 8
//22:17:56.096 [pool-1-thread-7] INFO com.aqs.CountDownLatchExample1 - current num 6
//22:17:56.095 [pool-1-thread-4] INFO com.aqs.CountDownLatchExample1 - current num 3
//22:17:56.095 [pool-1-thread-10] INFO com.aqs.CountDownLatchExample1 - current num 9
//22:17:56.095 [pool-1-thread-8] INFO com.aqs.CountDownLatchExample1 - current num 7
//22:17:56.095 [pool-1-thread-2] INFO com.aqs.CountDownLatchExample1 - current num 1
//22:17:56.095 [pool-1-thread-3] INFO com.aqs.CountDownLatchExample1 - current num 2
//22:17:56.095 [pool-1-thread-6] INFO com.aqs.CountDownLatchExample1 - current num 5
//22:17:56.095 [pool-1-thread-5] INFO com.aqs.CountDownLatchExample1 - current num 4
//22:17:56.100 [main] INFO com.aqs.CountDownLatchExample1 - all thread finish
指定阻塞時(shí)間
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author scottxuan
*/
@Slf4j
public class CountDownLatchExample2 {
private final static int threadCount = 10;
public static void main(String[] args) throws InterruptedException {
//初始化計(jì)數(shù)器,一般和線程數(shù)目相同
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < threadCount; i++) {
final int num = i;
executor.execute(()->{
count(num);
//多線程每執(zhí)行一次,計(jì)數(shù)器減1
countDownLatch.countDown();
});
}
//主線程阻塞指定的時(shí)間,改時(shí)間超過(guò)后繼續(xù)執(zhí)行后續(xù)的代碼,無(wú)論所有線程是否執(zhí)行完畢
countDownLatch.await(1000, TimeUnit.MILLISECONDS);
log.info("await time out");
executor.shutdown();
}
private static void count(int num){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("current num {}",num);
}
}
//輸出結(jié)果 線程的執(zhí)行每個(gè)需要2000ms,但1000ms之后阻塞釋放,先輸出
//22:24:10.016 [main] INFO com.aqs.CountDownLatchExample1 - await time out
//22:24:11.014 [pool-1-thread-10] INFO com.aqs.CountDownLatchExample1 - current num 9
//22:24:11.014 [pool-1-thread-2] INFO com.aqs.CountDownLatchExample1 - current num 1
//22:24:11.015 [pool-1-thread-4] INFO com.aqs.CountDownLatchExample1 - current num 3
//22:24:11.014 [pool-1-thread-9] INFO com.aqs.CountDownLatchExample1 - current num 8
//22:24:11.014 [pool-1-thread-1] INFO com.aqs.CountDownLatchExample1 - current num 0
//22:24:11.015 [pool-1-thread-3] INFO com.aqs.CountDownLatchExample1 - current num 2
//22:24:11.015 [pool-1-thread-8] INFO com.aqs.CountDownLatchExample1 - current num 7
//22:24:11.014 [pool-1-thread-6] INFO com.aqs.CountDownLatchExample1 - current num 5
//22:24:11.015 [pool-1-thread-7] INFO com.aqs.CountDownLatchExample1 - current num 6
//22:24:11.014 [pool-1-thread-5] INFO com.aqs.CountDownLatchExample1 - current num 4