Pages

Saturday, October 5, 2013

CountDownLatch tutorial: A class of java concurrency package

CoutDownLatch class is very popular interview question of multi-threading and concurrency control in java. Let's dig this more and find out all minute details about this. In this post I will cover many aspects including example of how it works, real time usages, possible interview questions on CountDownLatch. So here we go..

About CountDownLatch

CountDownLatch was introduced in JDK 1.5 under java.util.concurrent package. This class operates on threads. Let's see what Java says about this class-

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

So CountDownLatch provides way to synchronize threads in such a way that some threads will wait until all threads complete their execution. CountDownLatch works on counters So Idea is, you set counter value some value and some thread will be in waiting state till counter reaches to zero and some threads will be responsible to decrement counter by 1. For example say counter value is set to 3 and we tell JVM, “Hey JVM, Threads t1, t2, t3 will be in waiting state until counter reaches to zero and other threads t4, t5, t6 will be executing and in the will decrement the counter by one. ” So in this example threads t1,t2 and t3 will call countDownLatch.await() method and will move to waiting state and t4, t5 and t6 will be calling countDownLatch.countDown(). Method that will decrement counter by 1 and the moment counter reaches to zero, t1, t2 and t3 will move to ready to run state and will resume its execution once they get CPU cycle. One very important point about CountDownLatch is it's counter will decrease, it can not be reset again to count from start. So once it reaches to zero you can not use CountDownLatch class further.

How CountDownLatch works

CountDownLatch works on three basic principle:-
  • Create CountDownLatch object by passing count value in the constructor.
  • Call countDownLatch.await() on threads those you want to wait until counter reaches to zero.
  • Call countDownLatch.countDown() from threads those are in execution.
Let's see this by an example of Worker and Dispatcher. So responsibility of Dispatcher will be to start many worker Threads those will perform certain task and once all task is over Dispatcher will report saying all task assigned to worker threads are now over.
Let's see the coding part of this.
The Worker class
This class will take a CountDownLatch object and List of Worker threads as input. It's run method will spawn one thread for each worker and will go in waiting state by calling countDownLatch.await() method. Once all worker threads finish it's execution Dispatcher thread will again come into execution and print last line “All worker threads executed successfully. I may be now rest in peace.”
So That way, Dispatcher thread will be in waiting state and will wait for all worker threads to complete it's execution. Here is the Dispatcher class.
class Dispatcher implements Runnable{

    private final List workers;
    private CountDownLatch latch;

    Dispatcher(List workers, CountDownLatch latch){
        this.workers = workers;
        this.latch = latch;
    }
    @Override
    public void run() {
        System.out.println("Being a dispatcher, I am going to dispatch request to workers to finish their work now.");
        for (Worker worker : workers ){
            new Thread(worker).start();
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("All worker threads executed successfully. I may be now rest in peace.");
    }

}

Worker class
This class is doing simple stuff. This guy is simply running his work and increasing latch decreasing latch count down by 1.
class Worker implements Runnable {
    private String workerName;
    private final CountDownLatch latch;

    public Worker(String workerName, CountDownLatch latch) {
        this.workerName = workerName;
        this.latch = latch;
    }


    private void work() {
        System.out.println(workerName + " is working.");
    }

    @Override
    public void run() {
        work();
        latch.countDown();
    }
}

Main class
And finally the main class that prepares worker objects and invoke dispatcher to dispatch requests.
public class CountDownLatchExampleMain {

    private static final int NUMBER_OF_WORKER = 10;

    public static void main(String[] args) {
        System.out.println("Main thread starts it's execution.");
        CountDownLatch latch = new CountDownLatch(NUMBER_OF_WORKER);
        List workers = new ArrayList(NUMBER_OF_WORKER);
        for (int i = 1; i <= NUMBER_OF_WORKER; i ++ ) {
            workers.add(new Worker("worker-" + i, latch));
        }
        new Thread(new Dispatcher(workers, latch)).start();
        System.out.println("Main thread end it's execution.");
    }
}


Real life examples to use CountDownLatch

There are few example that one can consider to use CountDownLatch. Although there can be many more. I appreciate if you would like to share your use case in this post. It would be great for everyone to add more scenario.
Achieving maximum parallelism: One of the possible use case is, you have a test case that tests about how your common resource will behave if n number of thread tries to access that resource in same time. Or you have a singleton class and you want to test if n threads trying to create instance then only one thread should be able to successful and other thread should simply get object.
A niche thread synchronization: Communication between thread is a handy example to use CountDownLatch. Like the example I took above where dispatcher will wait for all the worker threads to complete their executions and finally print some message. Another example could be, you want to send a notification to the user that all worker threads are done with execution.

Interview questions on CountDownLatch

Here are some common interview questions those are asked on CountDownLatch. Feel free to add more. I'll appreciate that effort.
  • What is CountDownLatch and how it works?
  • Give a real life use case where CountDownLatch can be beneficial?
  • Differentiate CountDownLatch with CyclicBarrier?
I hope this post is helpful for you. Feel free to add comments or feedback, I'll appreciate it. Happy coding.