Java Concurrency (CountDownLatches)

Java Concurrency (CountDownLatches)

Hello readers, and welcome to yet another blog in the Java Concurrency series. Today, we are going to look at the Count Down Latch class in Java, what it is and how to use it. So, let’s dive straight into it.

CountDownLatch in Java

Sometimes, we have a need to start our application only when a particular set of tasks are complete. These tasks might be running in parallel and getting completed together or at different times. Then, how do we tell our other threads that all the tasks are completed? How do we keep track of which tasks are complete and which are not? CountDownLatch is a class just for that.

We can define a CountDownLatch in our program as a class that keeps a counter with itself. The counter's starting point is the number of threads that we need to wait for before we can notify other threads that they can start working. For example, if we need to wait for 5 tasks to be completed before other threads can start working, then the starting point for CountDownLatch will be 5. When a thread finishes its task, it can just call CountDownLatch's countDown() method to let it know that the task is finished. countDown() decrements the starting point, or the count, by 1. So, when the count reaches 0, the Latch knows that all the threads have finished their tasks, and the waiting threads can now proceed. The await() method is a blocking method of CountDownLatch, which blocks until the count down reaches to zero, after which the await() method returns immediately. Let us look at an example code.

import java.util.concurrent.CountDownLatch;
public class CountDownLatchDemo {
    private static final CountDownLatch COUNT_DOWN_LATCH = new CountDownLatch(5);
    public static void main(String[] args) {
        Thread run1 = new Thread(() -> {
            System.out.println("Doing some work..");
            try {
                Thread.sleep(2000);
                COUNT_DOWN_LATCH.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread run2 = new Thread(() -> {
            System.out.println("Doing some work..");
            try {
                Thread.sleep(2000);
                COUNT_DOWN_LATCH.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread run3 = new Thread(() -> {
            System.out.println("Doing some work..");
            try {
                Thread.sleep(2000);
                COUNT_DOWN_LATCH.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread run4 = new Thread(() -> {
            System.out.println("Doing some work..");
            try {
                Thread.sleep(2000);
                COUNT_DOWN_LATCH.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread run5 = new Thread(() -> {
            System.out.println("Doing some work..");
            try {
                Thread.sleep(3000);
                COUNT_DOWN_LATCH.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        run1.start();
        run2.start();
        run3.start();
        run4.start();
        run5.start();
        try {
            COUNT_DOWN_LATCH.await();
        } catch (InterruptedException e) {
            //Handle when a thread gets interrupted.
        }
        System.out.println("All tasks have finished..");
    }
}

In the above code, we have defined a COUNTDOWNLATCH with starting point of 5, which means that our aim is to wait for 5 threads to be completed before executing our normal flow. Then, in the main method, we have started 5 threads, all doing some work and calling countDown() when the work has finished.

Notice here, that we do not need to worry about making COUNTDOWNLATCH synchronized, or thread-safe, as the internal implementation of CountDownLatch class is already synchronized.

We call the await() method of CountDownLatch to wait till the counter reaches 0, and then we execute our normal code flow.

That was all about CountDownLatch. I hope this posts was helpful and that you learned something new today. Thank you for reading.

Further reading

☞ Java Programming Masterclass for Software Developers

☞ Selenium WebDriver with Java -Basics to Advanced+Frameworks

☞ Java In-Depth: Become a Complete Java Engineer!

☞ Top 4 Spring Annotations for Java Developer in 2019

☞ Java Tutorial for Absolute Beginners

☞ 100+ Java Interview Questions and Answers In 2019

☞ Python vs Java: Understand Object Oriented Programming

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company

Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]