首页 >Java >java教程 >在Java并发中,CountDownLatch和CyclicBarrier之间的区别是什么?

在Java并发中,CountDownLatch和CyclicBarrier之间的区别是什么?

WBOY
WBOY转载
2023-09-06 15:33:121279浏览

在Java并发中,CountDownLatch和CyclicBarrier之间的区别是什么?

CountDownLatch 和 CyclicBarrier 都用于多线程环境,并且它们都是多线程环境的一部分。

根据 Java Doc -

CountDownLatch - 一种允许一个或多个线程等待的同步辅助工具直到其他线程中执行的一组操作完成。

CyclicBarrier - 一种同步辅助工具,允许一组线程相互等待到达公共屏障点。

tr>
先生。编号 Key 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());
   }
}

以上是在Java并发中,CountDownLatch和CyclicBarrier之间的区别是什么?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除