CountDownLatch和CyclicBarrier都在多线程环境中使用,它们都是其中的一部分。
根据Java Doc-
CountDownLatch-同步辅助,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成。
CyclicBarrier-同步帮助,允许一组线程互相等待以到达一个公共的障碍点。
序号 | 键 | 循环屏障 | CountDownLatch |
---|---|---|---|
1 | 基本的 | 同步帮助,允许一组线程互相等待以到达一个公共的障碍点。 | 一种同步帮助,它允许一个或多个线程等待,直到在其他线程中执行的一组操作完成为止。 |
2 | 可笑的 | 它具有可以提供Runnable的构造函数。 | 它没有这样的构造函数 |
3 | 线程/任务 | 它维护线程数 | 它维护着许多任务 |
4。 | 一个先进的 | 这是不先进的 | 这是先进的。 |
5 | 例外 | 如果一个线程在等待时被中断,则所有其他等待线程将抛出B rokenBarrierException | 仅当前线程将抛出 InterruptedException。不会影响其他线程 |
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自动生成的捕获块 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自动生成的捕获块 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自动生成的捕获块 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自动生成的捕获块 e.printStackTrace(); } latch.countDown(); System.out.println("Services2"+latch.getCount()); } }