在Java並發中,CountDownLatch和CyclicBarrier之間的差異是什麼?
Sep 06, 2023 pm 03:33 PM
cyclicbarriercountdownlatchjava並發
CountDownLatch 和 CyclicBarrier 都用於多執行緒環境,並且它們都是多執行緒環境的一部分。
根據 Java Doc -
CountDownLatch - 允許一個或多個執行緒等待的同步輔助工具直到其他執行緒中執行的一組操作完成。
CyclicBarrier - 一種同步輔助工具,允許一組執行緒相互等待到達公共屏障點。
先生。編號 | Key | CyclicBarrier | CountDownLatch |
---|---|---|---|
#基本 | |||
#一種允許一個或多個執行緒的同步輔助工具等待其他執行緒中執行的一組操作完成。 | 可運行 | tr> | |
它有一個可以提供Runnable 的建構子。 | 它沒有這樣的建構子 | 3 | |
線程/任務 | 它維護執行緒計數 | 它維護任務計數 | 4. |
它不建議使用
######建議使用。 ##################5#########################如果一個執行緒等待時被中斷,則所有其他等待執行緒都會拋出B rokenBarrierException#############只有當前執行緒會拋出######InterruptedException,不會影響其他執行緒####### ##############CyclicBarrier 範例###public class Main { public static void main(String args[]) throws InterruptedException { ExecutorService executors = Executors.newFixedThreadPool(4); CyclicBarrier cyclicBarrier = new CyclicBarrier(5); executors.submit(new Service1(cyclicBarrier)); executors.submit(new Service1(cyclicBarrier)); executors.submit(new Service2(cyclicBarrier)); executors.submit(new Service2(cyclicBarrier)); executors.submit(new Service2(cyclicBarrier)); Thread.sleep(3000); System.out.println("Done"); } } import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class Service1 implements Runnable { CyclicBarrier cyclicBarrier; public Service1(CyclicBarrier cyclicBarrier) { super(); this.cyclicBarrier = cyclicBarrier; } @Override public void run() { System.out.println("Services1" + cyclicBarrier.getNumberWaiting()); while (true) { try { cyclicBarrier.await(); } catch (InterruptedException | BrokenBarrierException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class Service2 implements Runnable { CyclicBarrier cyclicBarrier; public Service2(CyclicBarrier cyclicBarrier) { super(); this.cyclicBarrier = cyclicBarrier; } @Override public void run() { System.out.println("Services2" + cyclicBarrier.getNumberWaiting()); while (true) { try { cyclicBarrier.await(); } catch (InterruptedException | BrokenBarrierException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }###CountDownLatch 範例###
public class Main { public static void main(String args[]) throws InterruptedException { ExecutorService executors = Executors.newFixedThreadPool(4); CountDownLatch latch= new CountDownLatch(2); executors.submit(new Service1(latch)); executors.submit(new Service2(latch)); latch.await(); System.out.println("Done"); } } import java.util.concurrent.CountDownLatch; public class Service1 implements Runnable { CountDownLatch latch; public Service1(CountDownLatch latch) { super(); this.latch = latch; } @Override public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } latch.countDown(); System.out.println("Services2"+latch.getCount()); } } import java.util.concurrent.CountDownLatch; public class Service2 implements Runnable { CountDownLatch latch; public Service2(CountDownLatch latch) { super(); this.latch = latch; } @Override public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } latch.countDown(); System.out.println("Services2"+latch.getCount()); } }###
以上是在Java並發中,CountDownLatch和CyclicBarrier之間的差異是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
刺客信條陰影:貝殼謎語解決方案
3 週前ByDDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
2 週前ByDDD
在哪裡可以找到原子中的起重機控制鑰匙卡
3 週前ByDDD
節省R.E.P.O.解釋(並保存文件)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影 - 如何找到鐵匠,解鎖武器和裝甲定制
4 週前ByDDD

熱工具

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。