Java 并发中 CountDownLatch 与 CyclicBarrier 之间的区别
CountDownLatch 和 CyclicBarrier 都用于多线程环境中,并且二者都属于。
根据 Java 文档 -
CountDownLatch - 一种同步辅助工具,可让一个或多个线程等待直到在其他线程中执行的一组操作完成。
CyclicBarrier - 一种同步辅助工具,可让一组线程全部等待彼此到达通用栅栏点。
序号 | 关键 | CyclicBarrier | CountDownLatch |
---|---|---|---|
1 | 基本 | 一种同步辅助工具,可让一组线程全部等待彼此到达通用栅栏点。 | 一种同步辅助工具,可让一个或多个线程等待直到在其他线程中执行的一组操作完成。 |
2 | 可运行 | 它有一个可以提供 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()); } }
广告