JUC之CountdownLatch使用詳解
其中構造參數(shù)用來初始化等待計數(shù)值,await() 用來等待計數(shù)歸零,countDown() 用來讓計數(shù)減一,需要的朋友可以參考下
一、是什么?
CountdownLatch 用來進行線程同步協(xié)作,等待所有線程完成倒計時。
其中構造參數(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(); }
四、 應用之同步等待多個遠程調用結束
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(); }
到此這篇關于JUC之CountdownLatch使用詳解的文章就介紹到這了,更多相關CountdownLatch使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot自帶的緩存@EnableCaching用法
這篇文章主要介紹了springboot自帶的緩存@EnableCaching用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Java?DelayQueue實現(xiàn)延時任務的示例詳解
DelayQueue是一個無界的BlockingQueue的實現(xiàn)類,用于放置實現(xiàn)了Delayed接口的對象,其中的對象只能在其到期時才能從隊列中取走。本文就來利用DelayQueue實現(xiàn)延時任務,感興趣的可以了解一下2022-08-08eclipse創(chuàng)建一個基于maven的web項目詳細步驟
開始學習maven,并用maven創(chuàng)建了第一個屬于自己的web項目,下面這篇文章主要給大家介紹了關于eclipse創(chuàng)建一個基于maven的web項目的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-12-12Redisson 分布式延時隊列 RedissonDelayedQueue 運行流程
這篇文章主要介紹了Redisson分布式延時隊列 RedissonDelayedQueue運行流程,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09Java上傳文件進度條的實現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了Java上傳文件進度條的實現(xiàn)方法,可簡單實現(xiàn)顯示文件上傳比特數(shù)及進度的功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2015-12-12