Was it helpful?

Question


CountDownLatch and CyclicBarrier both used in multithreading environment and they both are part of.

As per Java Doc −

CountDownLatch − A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

CyclicBarrier − A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.

Sr. No.KeyCyclicBarrierCountDownLatch
1
Basic
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.


A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.


2
Ruunable
It has a constructor where Runnable can be provided.
It don’t have such constructor
3
Thread /Task
It maintains a count of threads
It maintains a count of tasks
4.
A dvanceable
It is not advancable
It is advancable.
5
Exception
If one thread is interrupted while waiting then all other waiting threads will throw B rokenBarrierException
Only current thread will throw
InterruptedException. It will not impact other threads

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

Example of 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());
   }
}
raja
Published on 09-Sep-2020 11:54:07

Was it helpful?
Not affiliated with Tutorialspoint
scroll top