A CountDownLatch Example Explained

A CountDownLatch is a thread synchronization construct that held current thread till the count of the latch reaches to zero. We can assume it to be a lock on main thread which opens only when its count reaches to zero.

There are three things which we need to understand to know about this.
• Initial Count
await() method
countdown() method

Initial count is generally the total number of threads.
When a thread call awaits on a countdown latch object, the flow of the current threads stops, till count reaches to zero. 
Each thread call countdown() after finishing the work which decrease the count of a latch.

Lets consider below example:-

There are two sets of CountDownLatch used below.

startSignal:-

startSignal is used to make sure that all threads starts at same time. When we call .start()on the threads all threads will wait at “startSignal.await();”. These threads will not proceed till count on startThread reaches zero which happens at “startSignal.countDown();” When “startSignal.countDown();” is called count of that latch reaches zero and hence all threads process from “startSignal.await();”.

endSignal:-

This latch make sure that main thread continues only after all threads finished. Here we initialize endSignal with number of threads. We pass endSignal object to each thread. Each thread continue its work and decrease the count. When Main thread calls “endSignal.await();” The main thread will wait till endSignal count reaches zero. Each thread will end their work and call “endSignal.countDown();” which decreases count. Main thread will continue only when count reaches to zero, which means all thread have executed “endSignal.countDown();” which means all thread finished their work.


import java.util.concurrent.CountDownLatch;
public class CountDownLatchDemo
{
private static final int THREAD_COUNT = 5;
public static void main(String[] args) throws InterruptedException
{
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch endSignal = new CountDownLatch(THREAD_COUNT);

for(int i=0; i< THREAD_COUNT; i++){
new Thread(new Worker(startSignal, endSignal), "Thread:"+i).start();
}
System.out.println("Lets start all thread at once..");
startSignal.countDown(); // let all threads proceed
System.out.println("All threadsa are running..");
System.out.println("Lets wait all thread to finish..");
endSignal.await(); // wait for all threads to finish
System.out.println("All threads finished..Main thread terminited");
}
}

class Worker implements Runnable {
private CountDownLatch startSignal;
private CountDownLatch endSignal;

public Worker(CountDownLatch start, CountDownLatch end) {
startSignal = start;
endSignal = end;
}
public void run() {
final String threadName = Thread.currentThread().getName();
try{
startSignal.await();
for(int i=0;i < 3; i++){
System.out.println("Thread:"+ threadName + " is working");
try{
Thread.sleep((int)(Math.random()*300));
}catch (InterruptedException ie){
}
}
System.out.println("Thread:"+ threadName + " is finishing");
//Indicate end of a thread.
endSignal.countDown();
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
}

Output:

Lets start all thread at once..
All threadsa are running..
Lets wait all thread to finish..
Thread:Thread:1 is working
Thread:Thread:2 is working
Thread:Thread:0 is working
Thread:Thread:3 is working
Thread:Thread:4 is working
Thread:Thread:4 is working
Thread:Thread:4 is working
Thread:Thread:4 is finishing
Thread:Thread:0 is working
Thread:Thread:2 is working
Thread:Thread:1 is working
Thread:Thread:3 is working
Thread:Thread:2 is working
Thread:Thread:0 is working
Thread:Thread:2 is finishing
Thread:Thread:1 is working
Thread:Thread:3 is working
Thread:Thread:1 is finishing
Thread:Thread:0 is finishing
Thread:Thread:3 is finishing
All threads finished..Main thread terminited

Leave a comment