Java CountDownLatch計(jì)數(shù)器與CyclicBarrier循環(huán)屏障
定義
CountDownLatch: A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
CyclicBarrier: A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.
上述是Oracle官方定義。簡單來說
CountDownLatch:計(jì)數(shù)器,允許一個或多個線程等待,直到在其他線程中執(zhí)行的一組操作完成。
CyclicBarrier:循環(huán)屏障,它允許一組線程相互等待以達(dá)到一個共同的屏障點(diǎn)。
區(qū)別
- CountDownLatch 是 AQS (AbstractQueuedSynchronizer) 的一員,但 CyclicBarrier 不是。
- CountDownLatch 的使用場景中,有兩類線程,一類是調(diào)用await()方法的等待線程,另一類是調(diào)用countDownl() 方法的操作線程。CyclicBarrier 的場景中,只有一類線程,都是相互等待的等待線程。
- CountDownLatch 是減計(jì)數(shù),遞減完后不能復(fù)位,CyclicBarrier 是加計(jì)數(shù),遞增完后自動復(fù)位
CountDownLatch 示例
創(chuàng)建兩組線程,一組等待另一組執(zhí)行完才繼續(xù)進(jìn)行
CountDownLatch countDownLatch = new CountDownLatch(5); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { executorService.execute(() -> { countDownLatch.countDown(); System.out.println("run.."); }); } for (int i = 0; i < 3; i++) { //我們要等上面執(zhí)行完成才繼續(xù) executorService.execute(() -> { try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("await.."); }); } executorService.shutdown();
打?。?/p>
run..
run..
run..
run..
run..
await..
await..
await..
等待累加線程執(zhí)行完,主線程再輸出累加結(jié)果
public class ThreadUnsafeExample { private int cnt = 0; public void add() { cnt++; } public int get() { return cnt; } public static void main(String[] args) throws InterruptedException { final int threadSize = 1000; ThreadUnsafeExample example = new ThreadUnsafeExample(); final CountDownLatch countDownLatch = new CountDownLatch(threadSize); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < threadSize; i++) { executorService.execute(() -> { example.add(); countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println(example.get()); } }
打?。?/p>
997
3 模擬并發(fā)
ExecutorService executorService = Executors.newCachedThreadPool(); CountDownLatch countDownLatch = new CountDownLatch(1); for (int i = 0; i < 5; i++) { executorService.submit( () -> { try { countDownLatch.await(); System.out.println("【" + Thread.currentThread().getName() + "】開始執(zhí)行……"); } catch (InterruptedException e) { e.printStackTrace(); } }); } Thread.sleep(2000); countDownLatch.countDown();//開始并發(fā) executorService.shutdown();
打?。?/p>
【pool-2-thread-2】開始執(zhí)行……
【pool-2-thread-5】開始執(zhí)行……
【pool-2-thread-3】開始執(zhí)行……
【pool-2-thread-1】開始執(zhí)行……
【pool-2-thread-4】開始執(zhí)行……
CyclicBarrier 示例
所有線程相互等待,直到某一步完成后再繼續(xù)執(zhí)行
final int totalThread = 3; CyclicBarrier cyclicBarrier = new CyclicBarrier(3); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < totalThread; i++) { executorService.execute(() -> { System.out.println("before.."); try { cyclicBarrier.await(); } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } System.out.println("after.."); }); } executorService.shutdown();
打?。?/p>
before..
before..
before..
after..
after..
after..
到此這篇關(guān)于Java CountDownLatch計(jì)數(shù)器與CyclicBarrier循環(huán)屏障的文章就介紹到這了,更多相關(guān)Java CountDownLatch與CyclicBarrier內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java常用HASH算法總結(jié)【經(jīng)典實(shí)例】
這篇文章主要介紹了Java常用HASH算法,結(jié)合實(shí)例形式總結(jié)分析了Java常用的Hash算法,包括加法hash、旋轉(zhuǎn)hash、FNV算法、RS算法hash、PJW算法、ELF算法、BKDR算法、SDBM算法、DJB算法、DEK算法、AP算法等,需要的朋友可以參考下2017-09-09java靜態(tài)工具類注入service出現(xiàn)NullPointerException異常處理
如果我們要在我們自己封裝的Utils工具類中或者非controller普通類中使用@Autowired注解注入Service或者M(jìn)apper接口,直接注入是報(bào)錯的,因Utils用了靜態(tài)方法,我們無法直接用非靜態(tài)接口的,遇到這問題,我們要想法解決,下面小編就簡單介紹解決辦法,需要的朋友可參考下2021-09-09SpringBoot集成redis實(shí)現(xiàn)分布式鎖的示例代碼
這篇文章主要介紹了SpringBoot集成redis實(shí)現(xiàn)分布式鎖的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java-Redis-Redisson分布式鎖的功能使用及實(shí)現(xiàn)
這篇文章主要介紹了Java-Redis-Redisson-分布式鎖的功能使用及實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08Java中實(shí)現(xiàn)漢字生成拼音首拼和五筆碼
這篇文章主要介紹了Java中實(shí)現(xiàn)漢字生成拼音首拼和五筆碼方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java實(shí)現(xiàn)計(jì)算一個月有多少天和多少周
這篇文章主要介紹了Java實(shí)現(xiàn)計(jì)算一個月有多少天和多少周,本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-06-06