Home  >  Article  >  Java  >  What is the difference between CountDownLatch and CyclicBarrier in Java concurrency?

What is the difference between CountDownLatch and CyclicBarrier in Java concurrency?

WBOY
WBOYforward
2023-09-06 15:33:121216browse

What is the difference between CountDownLatch and CyclicBarrier in Java concurrency?

CountDownLatch and CyclicBarrier are both used in and are part of a multi-threaded environment.

According to Java Doc -

CountDownLatch - A synchronization aid that allows one or more threads to wait until a set of operations performed in other threads is completed.

CyclicBarrier - A synchronization aid that allows a group of threads to wait for each other to reach a common barrier point.

##2RunningIt has a constructor that can provide Runnable. It has no such constructor3Thread /taskIt maintains thread countIt maintains task count tr>4.5CyclicBarrier Example
gentlemen. No. Key CyclicBarrier CountDownLatch
1

Basic

Allows synchronization of a group of threads all waiting for each other to reach a common obstacle point.

A synchronization aid that allows one or more threads to wait for a set of operations performed in other threads to complete.

##Can be advanced

It is not recommended to use

It is recommended to use.

Exception

If a thread is blocked while waiting If interrupted, all other waiting threads will throw BrokenBarrierException

Only the current thread will throw

InterruptedException and will not affect other threads

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 Example

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());
   }
}

The above is the detailed content of What is the difference between CountDownLatch and CyclicBarrier in Java concurrency?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete