CountdownLatch

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");
 

         thread1.start();
         thread2.start();
         thread3.start();
         thread4.start();
         thread5.start();
 

         System.out.println("Father going to wait for servant thread to complete");
         countdownLatch.await();
         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;
         this.setName(servantName);
     }
 

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

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s