CountDownLatch

Countdown latch is a thread-synchronization construct that causes one or more threads to wait until a set of operations being performed by other threads finishes. It consists of a count and "cause a thread to wait until the count reaches zero" and "decrement the count" operations.

The java.util.concurrent.CountDownLatch class implements a countdown latch. Its CountDownLatch(int count) constructor initializes the countdown latch to the specified count. A thread invokes the void await() method to wait until the count has reached zero (or the thread has been interrupted). Subsequent calls to await() for a zero count return immediately. A thread calls void countDown() to decrement the count.

Working using CountDownLatch

The await() methods block the current threads until the count reaches 0 due to invocations of the countDown() method by some other thread, after which blocked threads are released.

Lets see an example to make the concept more clear :

In our example, we are setting up a server and to setup a server we need to start 3 services : database service, cache service and network service . Once all the service validation is done,our server is up.

CountdownLatchExample.java
public class CountdownLatchExample {

    public static void main(String args[]) {
        final CountDownLatch countDownLatch = new CountDownLatch(3);
        Thread databaseService = new Thread(new Service("DatabaseService", countDownLatch));
        Thread networkService = new Thread(new Service("NetworkService", countDownLatch));
        Thread cacheService = new Thread(new Service("CacheService", countDownLatch));

        databaseService.start();
        networkService.start();
        cacheService.start();

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("External service validation completed !!");
    }
}

Service.java
public class Service implements Runnable {

    String name;
    CountDownLatch countDownLatch;

    Service(String name, CountDownLatch countDownLatch) {
        this.name = name;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        System.out.println(name + " is starting ...");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name + " is UP !");
        countDownLatch.countDown();
    }
}

Output:
NetworkService is starting ...
DatabaseService is starting ...
CacheService is starting ...
DatabaseService is UP !
NetworkService is UP !
CacheService is UP !
External service validation completed !!

As per the output ,we can see that initially all the services do their work and decrement the countDownLatch , once countDownLatch value reaches to 0. Our external service validation gets complete and we can start our work.

Applications of  CountDownLatch  :

  • CountDownLatch is used mainly in scenarios as explained in our example, when we have to start our server and before starting the server we have to do validation  of other services.


This covers the concept of CountDownLatch in JavaConcurrency . If you have any question or you want to add anything then please comment below.

Comments

Popular posts from this blog

Deploy standalone Spring MVC Application in Docker Container

Refactor Code : Separate Query from Modifier

ConcurrentHashMap Internal Working