Java concurrency – CountDownLatch Real Time Example - Java @ Desk

Saturday, December 8, 2018

Java concurrency – CountDownLatch Real Time Example

Java concurrency – CountDownLatch Example

CountDownLatch is the class introduced in Concurrency API in JDK 1.5. CountDownLatch allows the application to wait for the rest of the threads to get over. In a scenario, it is required a single primary thread to wait until all the thread finishes its processing. To achieve this, CountDownLatch is used.

CountDownLatch is initialized with an integer value. This integet value suggests, for how many count down it needs to wait. Every time a thread calls "countDown()" method, the initialized count reduces by 1. The moment count reaches 0, main thread resumes further.

Different Scenarios -
1) If CountDownLatch is initialized with 0, and "countDown()" is called 3 times in different or same threads - Client does not wait since the count itself is 0 during initialization.
2) If CountDownLatch is initialized with 1, and "countDown()" is called 3 times in different or same threads - Client will move forward as soon as 1st "countDown()" is called.
3) If CountDownLatch is initialized with 3, and "countDown()" is called 3 times in different or same threads - Client will move forward only after 3rd "countDown()" is called.

Real Time Scenario -
Consider a Bank that works from morning 9 AM to 3 PM. Bank will be closed only when it serves all the clients that has entered before 3 PM. Bank will remain open unless all customers leave the premises.

In this case, Bank is a CountDownLatch Main Client on which "await()" method is applied and Customers are the ones who performs "countDown()" on the latch.

Here is a realtime example of CountDownLatch
CountDownLatchClient.java - It creates the CountDownLatch object with count 2 and enters in await() state for threads to call "countDown()" atleast twice. 2 threads will be created and CountDownLatchClient will wait for them to finish processing.
package com.learning.countdownlotch;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchClient {

    public static void main(String args[]) {

        final CountDownLatch latch = new CountDownLatch(2);

        CountDownThread countDownThreadOne = new CountDownThread();
        countDownThreadOne.setLatch(latch);
        countDownThreadOne.setCount(1);
        countDownThreadOne.setTime(1000);

        new Thread(countDownThreadOne).start();

        CountDownThread countDownThreadTwo = new CountDownThread();
        countDownThreadTwo.setLatch(latch);
        countDownThreadTwo.setCount(2);
        countDownThreadTwo.setTime(3000);

        new Thread(countDownThreadTwo).start();

        try {
            latch.await();
            System.out.println("Main Ends");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


CountDownThread.java - This is a thread class which calls the "countDown()" on the CountDownLatch Object
package com.learning.countdownlotch;

import java.util.concurrent.CountDownLatch;

public class CountDownThread implements Runnable {
    private CountDownLatch latch;

    private int count;

    private int time;

    public void run() {

        try {
            System.out.println("CountDownThread Starts " + getCount());
            Thread.sleep(time);
            System.out.println("CountDownThread Ends " + getCount());
            latch.countDown(); //reduce count of CountDownLatch by 1
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public CountDownLatch getLatch() {
        return latch;
    }

    public void setLatch(CountDownLatch latch) {
        this.latch = latch;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }
}


Output -
CountDownThread Starts 1
CountDownThread Starts 2
CountDownThread Ends 1
CountDownThread Ends 2
Main Ends






No comments:

Post a Comment