Deep Dive CountDownLatch in Java

Vivek Shivhare
Level Up Coding
Published in
3 min readOct 13, 2020

--

CountDownLatch in Java
CountDownLatch in Java

What?

CountDownLatch in Java is a kind of synchronization mechanism which allows one Thread to wait for one or more Threads before starts processing. This is a very crucial requirement and often needed in server-side application and having this functionality built-in as CountDownLatch greatly simplifies the development.

Why?

Similar behavior can be achieved using wait and notify in Java but it requires efforts, expertise and getting it to write in the first attempt is tricky, With CountDownLatch it can be done in just a few lines. CountDownLatch also allows flexibility on a number of threads for which the main thread should wait, It can wait for one thread or n number of threads, there is not much change on code.

CountDownLatch protects you against the case of a thread missing a signal which can occur if you use these other mechanisms for coordinating jobs. Something like a Condition is useful for signaling to threads if they are waiting, but where it doesn’t matter if they’re not, or where a thread will explicitly check if it has to wait before waiting. With a CountDownLatch, we await a signal if it hasn’t been triggered yet, but immediately continue without waiting if that signal was already triggered before we start waiting.

When?

CountDownLatch is instrumental when one of Thread like main thread, require to wait for one or more thread to complete before its start doing the processing. A classical real life example of using CountDownLatch in Java is when we distribute a file into fixed number of chunks and we send these chunk of data to different thread to process after all have done the processing then only the File should be closed.

Lab

Consider the example where an Organization needs to recruit three Developers. For this HR Manager has asked three Tech Leads to take interview. The HR Manager wants to distribute the Offer letter only after all the three Developers have been recruited. In Threading terminology the HR Manger should wait till three Developers have been recruited.

import java.util.concurrent.CountDownLatch;

public class HRManager{

public static void main(String args[]){
CountDownLatch countDownLatch =newCountDownLatch(3);

TechLead techLead1 =new TechLead(countDownLatch,”first”);
TechLead techLead2 =new TechLead(countDownLatch,”second”);
TechLead techLead3 =new TechLead(countDownLatch,”third”);

techLead1.start();
techLead2.start();
techLead3.start();

try{
System.out.println(“HR Manager waiting for recruitment to complete…”);

countDownLatch.await();

System.out.println(“Distribute Offer Letter”);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}

Now lets create Tech Lead’s who will take the interviews

import java.util.concurrent.CountDownLatch;

public class TechLead extends Thread{

CountDownLatch countDownLatch;
public TechLead(CountDownLatch countDownLatch,String name){
super(name);
this.countDownLatch=countDownLatch;

}

@Override
publicvoid run(){
try{

Thread.sleep(2000);
}catch(InterruptedException e){

e.printStackTrace();
}

System.out.println(Thread.currentThread().getName()+” : recruited”);

countDownLatch.countDown();
}

}

Beware of monopoly !

CountDownLatch is a very handy class to achieve synchronization between two or more threads, where allows one or more threads to wait until a set of operations being performed in other threads completes. CountDownLatch can save your time in suitable cases and you have to be aware of this class. As always synchronization of threads can raise deadlocks if code is not good.

Thanks for reading 💜

--

--

Technologist | Blogger | FinTech | Bank of America | Solutions Architecture | Merchant Services | Data & Cloud Strategy | www.linkedin.com/in/shivharevivek