CyclicBarrier Example

In the last chapter Java CountDownLatch Example we saw how a master or main thread waits till the worker threads finish their work. CyclicBarrier class also is a flavour of CountdownLatch with slight change. Lets continue the same example as CountDownLatch.
A organization has to recruit 3 Java Developers. And so the HR Manager asks 3 Tech Leads to interview the candidates. In CountDownLatch example the HR Manager wanted to distribute the offer letter to all the 3 candidates that is the reason we made him to wait. Here the HR manager wants the Tech Leads to give the offer letter once they have selected the candidate.
But the Tech Leads decide among themselves that they will give the offer letter to their respective candidate only when all interviews are done.
Lets simulate this using CyclicBarrier.

 HR Manager or Main Thread class

import java.util.concurrent.CyclicBarrier;


public class HRManager {
 
 public static void main(String args[]){
  CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
  
  TechLead techLead1 = new TechLead(cyclicBarrier,"John TL");
  TechLead techLead2 = new TechLead(cyclicBarrier,"Doe TL");
  TechLead techLead3 = new TechLead(cyclicBarrier,"Mark TL");
  
  
  techLead1.start();
  techLead2.start();
  techLead3.start();
  
  System.out.println("No work for HR manager");
  
 }

}

TechLead or Worker Thread class


import java.util.concurrent.CyclicBarrier;

public class TechLead extends Thread {

 CyclicBarrier cyclicBarrier;

 public TechLead(CyclicBarrier cyclicBarrier, String name) {
  super(name);
  this.cyclicBarrier = cyclicBarrier;

 }

 @Override
 public void run() {

  try {
   Thread.sleep(3000);
   System.out.println(Thread.currentThread().getName() + " recruited developer");
   System.out.println(Thread.currentThread().getName() + " waiting for others to complete....");
   cyclicBarrier.await();
   System.out.println("All finished recruiting, " + Thread.currentThread().getName()
     + " gives offer letter to candidate");

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Here the HR manager just starts the TechLeads thread and exists. It is not waiting for anything. The TechLeads are responsible to take the interviews.
The HR manager created a instance of Cyclicbarrier class and passed it in the constructor of TechLeads, so every TeachLead has the same instance of cyvlicbarrier class. After recruting their candidate the Tech Leads call cyclicBarrier.await() method which will stop their execution and they will go in waiting state.
Till all the Tech Leads have not called cyclicBarrier.await(); the TechLeads will wait for eachother. When all reach a pint where everyone has done recruitment the all will come back to runnable state and then give the offer letter.

Lets run the example and see what the output is

No work for HR manager
Mark TL recruited developer
Mark TL waiting for others to complete....
John TL recruited developer
John TL waiting for others to complete....
Doe TL recruited developer
Doe TL waiting for others to complete....
All finished recruiting, Doe TL gives offer letter to candidate
All finished recruiting, John TL gives offer letter to candidate
All finished recruiting, Mark TL gives offer letter to candidate

Important Methods

CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

This is a way to create the instance of CyclicBarrier class. The parameter '3' mentions number of parties or worker threads. The threads will wait till these many number of thread call await() method on the same CyclicBarrier instance

cyclicBarrier.await();


When this method is called the current thread which called this method will go into waiting state and will wake up or come to runnable only when all parties in out case 3 parties have call the await() method.

Java Thread class Example >> Java Runnable Interface Example >> Java Callable Interface Example  >> java CountDownLatch Example  >> Java CyclicBarrier Example  >> Semaphore

5 comments:

  1. Hello Hunaid,
    Really nice aricle,can you please explain how CountDownLatch and CyclicBarrier internally works and if there is any drawback of them.

    ReplyDelete
  2. Excellent explanation. Please add blog on Future as well.

    ReplyDelete
  3. Hi Hunaid, thanks for your nice article.If possible can you attach drawbacks and exact use cases also.I hope you understand.

    ReplyDelete
  4. What about reset method of CyclicBarrier.

    ReplyDelete

Share the post