CountDownLatch 和 CyclicBarrier 都用於多執行緒環境,並且它們都是多執行緒環境的一部分。
根據 Java Doc -
CountDownLatch - 允許一個或多個執行緒等待的同步輔助工具直到其他執行緒中執行的一組操作完成。
CyclicBarrier - 一種同步輔助工具,允許一組執行緒相互等待到達公共屏障點。
先生。編號 | Key | CyclicBarrier | CountDownLatch |
---|---|---|---|
#基本 | |||
#一種允許一個或多個執行緒的同步輔助工具等待其他執行緒中執行的一組操作完成。 | 可運行 | tr> | |
它有一個可以提供Runnable 的建構子。 | 它沒有這樣的建構子 | 3 | |
線程/任務 | 它維護執行緒計數 | 它維護任務計數 | 4. |
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(); } } } }
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中文網其他相關文章!