Explain CountDownLatch using real life problem?

Let me try to explain CountDownLatch with some real life example:

Consider you 5 rooms in your house. Your father is at home with 5 servants, and you are calling from office to your father to ask him to check if you have switched off the light of each room before leaving and call you back.

So your father will ask 5 servants to go in each room and check for the lights and will wait for all servants to come back. Since all servants works at different speed therefore they will reach to your father at different times, and your father need to wait for servants to return before calling you to update you.

In coding how you will do it is: Your father will create a CountDownLatch object and give it count value as 5, and will start 5 servant threads and pass them CountDownLatch object. Your father will then calls await method on latch object, which will make him wait till CountDownLatch value is reduced to 0.

Now each servant thread will do its work (check room) and after that will call countDown() method on latch object, which will internall cause count value associated with latch object to reduce by 1.

When all servants have done their work, this means that count value is 0, which means father will automatically wake up and does his next job, that is to call you.

Can you write sample code for the above father, servant problem?

public class FatherServantProblem {

     public static void main(String args[]) throws Exception {

         System.out.println("Main thread is Father thread");

         CountDownLatch countdownLatch = new CountDownLatch(5);

         ServantThread thread1 = new ServantThread(countdownLatch, 1, "Servant 1");
         ServantThread thread2 = new ServantThread(countdownLatch, 2, "Servant 2");
         ServantThread thread3 = new ServantThread(countdownLatch, 3, "Servant 3");
         ServantThread thread4 = new ServantThread(countdownLatch, 4, "Servant 4");
         ServantThread thread5 = new ServantThread(countdownLatch, 5, "Servant 5");
         System.out.println("Father sending servants to all rooms");


         System.out.println("Father going to wait for servant thread to complete");
         System.out.println("Father calling child");


 class ServantThread extends Thread {

     private CountDownLatch countDownLatch;
     private int roomNo;
     private String name;

     public ServantThread(CountDownLatch countDownLatch, int roomNo, String servantName) {
         this.countDownLatch = countDownLatch;
         this.roomNo = roomNo;
         this.name = servantName;

     public void run() {
         System.out.println(name + " checking room: " + roomNo);
         try {
         } catch (InterruptedException e) {
         System.out.println(name + " reducing count");

Can you give me technical definition of CountDownLatch now?

It is an API which allows one or more threads (there can be more than one father calling await) to wait until a set of operations being performed in other threads completes.

A CountDownLatch is initialized with a given count.

The await methods block the thread calling it until the current count reaches zero. Count decreases when operations/threads invockes countDown(). Once the count is zero and any thread invokes await call i returned immediately.

Can the count be reset?

This is a one-shot phenomenon — the count cannot be reset.

How to use CountDownLatch as on/off latch or gate?

Consider the count value is 1 for CountDownLatch. There are number of threads which will call await(), therefore number of threads waiting at CountDownLatch.
There is only one thread which will invoke countDown(), which causes count value to go 0, and all threads calling await() will be unblocked. Here CountDownLatch is acting as a gate, causing many threads to wait.

Rakesh Kalra

Hello, I am Rakesh Kalra. I have more than 15 years of experience working on IT projects, where I have worked on varied complexity of projects and at different levels of roles. I have tried starting my own startups, 3 of those though none of it were successful but gained so much knowledge about business, customers, and the digital world. I love to travel, spend time with my family, and read self-development books.


Leave a Reply