Hello readers, and welcome to yet another blog in the Java Concurrency series. Today, we are going to look at the CountDownLatch 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 which 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 it’s 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 to 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.
In the above code, we have defined a COUNT_DOWN_LATCH 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 COUNT_DOWN_LATCH 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 blog was helpful for you and you got to learn something new today. Thank you for reading and happy blogging!