JUC之CountdownLatch使用詳解
其中構(gòu)造參數(shù)用來初始化等待計數(shù)值,await() 用來等待計數(shù)歸零,countDown() 用來讓計數(shù)減一,需要的朋友可以參考下
一、是什么?
CountdownLatch 用來進行線程同步協(xié)作,等待所有線程完成倒計時。
其中構(gòu)造參數(shù)用來初始化等待計數(shù)值,await() 用來等待計數(shù)歸零,countDown() 用來讓計數(shù)減一
二、demo演示
public class TestCountDownLatch {
public static void main(String[] args) throws InterruptedException, ExecutionException {
test5();
}
private static void test5() {
CountDownLatch latch = new CountDownLatch(3);
ExecutorService service = Executors.newFixedThreadPool(4);
service.submit(() -> {
log.debug("begin...");
sleep(1);
latch.countDown();
log.debug("end...{}", latch.getCount());
});
service.submit(() -> {
log.debug("begin...");
sleep(1.5);
latch.countDown();
log.debug("end...{}", latch.getCount());
});
service.submit(() -> {
log.debug("begin...");
sleep(2);
latch.countDown();
log.debug("end...{}", latch.getCount());
});
service.submit(()->{
try {
log.debug("waiting...");
latch.await();
log.debug("wait end...");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
三、應用之同步等待多線程準備完畢
private static void test2() throws InterruptedException {
AtomicInteger num = new AtomicInteger(0);
ExecutorService service = Executors.newFixedThreadPool(10, (r) -> {
return new Thread(r, "t" + num.getAndIncrement());
});
CountDownLatch latch = new CountDownLatch(10);
String[] all = new String[10];
Random r = new Random();
for (int j = 0; j < 10; j++) {
int x = j;
service.submit(() -> {
for (int i = 0; i <= 100; i++) {
try {
Thread.sleep(r.nextInt(100));
} catch (InterruptedException e) {
}
all[x] = Thread.currentThread().getName() + "(" + (i + "%") + ")";
System.out.print("\r" + Arrays.toString(all));
}
latch.countDown();
});
}
latch.await();
System.out.println("\n游戲開始...");
service.shutdown();
}四、 應用之同步等待多個遠程調(diào)用結(jié)束
private static void test3() throws InterruptedException, ExecutionException {
RestTemplate restTemplate = new RestTemplate();
log.debug("begin");
ExecutorService service = Executors.newCachedThreadPool();
CountDownLatch latch = new CountDownLatch(4);
service.submit(() -> {
Map<String, Object> response = restTemplate.getForObject("http://localhost:8080/order/{1}", Map.class, 1);
log.debug("{}",response);
latch.countDown();
});
service.submit(() -> {
Map<String, Object> response1 = restTemplate.getForObject("http://localhost:8080/product/{1}", Map.class, 1);
log.debug("{}",response1);
latch.countDown();
});
service.submit(() -> {
Map<String, Object> response2 = restTemplate.getForObject("http://localhost:8080/product/{1}", Map.class, 2);
log.debug("{}",response2);
latch.countDown();
});
service.submit(() -> {
Map<String, Object> response3 = restTemplate.getForObject("http://localhost:8080/logistics/{1}", Map.class, 1);
log.debug("{}",response3);
latch.countDown();
});
latch.await();
log.debug("執(zhí)行完畢");
service.shutdown();
}到此這篇關(guān)于JUC之CountdownLatch使用詳解的文章就介紹到這了,更多相關(guān)CountdownLatch使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PageHelper引發(fā)的幽靈數(shù)據(jù)問題解析
這篇文章主要為大家介紹了PageHelper引發(fā)的幽靈數(shù)據(jù)問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
Java線性結(jié)構(gòu)中的雙向鏈表實現(xiàn)原理
這篇文章將給大家詳細講解雙向鏈表的內(nèi)容,尤其是會通過代碼來進行鏈表的操作,文中的代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-07-07
Java SE使用數(shù)組實現(xiàn)高速數(shù)字轉(zhuǎn)換功能
隨著大數(shù)據(jù)時代的到來,數(shù)字轉(zhuǎn)換功能變得越來越重要,在Java開發(fā)中,數(shù)字轉(zhuǎn)換功能也是經(jīng)常用到的,下面我們就來學習一下如何使用Java SE數(shù)組實現(xiàn)高速的數(shù)字轉(zhuǎn)換功能吧2023-11-11
在Windows上實現(xiàn)多JDK快速切換的高效方法
在現(xiàn)代軟件開發(fā)過程中,Java作為一種廣泛應用的編程語言,其不同版本之間的兼容性和功能差異使得開發(fā)者經(jīng)常需要在多個JDK版本之間進行切換,這篇文章主要介紹了在Windows上實現(xiàn)多JDK快速切換的高效方法,需要的朋友可以參考下2026-01-01
一小時迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API
這篇文章主要介紹了一小時迅速入門Mybatis之bind與多數(shù)據(jù)源支持 Java API,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
在SpringBoot項目中使用Spring Cloud Sentinel實現(xiàn)流量控制
隨著微服務架構(gòu)的流行,服務之間的調(diào)用變得越來越頻繁和復雜,流量控制是保障系統(tǒng)穩(wěn)定性的重要手段之一,它可以幫助我們避免因過載而導致的服務不可用,本文將介紹如何在Spring Boot項目中使用Spring Cloud Sentinel來實現(xiàn)流量控制,需要的朋友可以參考下2024-08-08

