Countdown Latch
- Eclipse: Oxygen
- Java: 1.8
This tutorial deals with the countdown latch. The basic use of this is that we can place the thread that we want to execute after a certain number of threads. This can be done by setting the countdown number to the number of threads after which we want the specific thread to be executed.
In the following program, we see the inconsistency in our program where the main thread is not waiting for other threads before it starts.
Here we create a task that is going to wait for four threads before it starts. Then we create four threads and start them. As we can see the inconsistency in our program where the main thread is not waiting for other threads before it starts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
class SomeThread extends Thread{ private CountDownLatch latch; public SomeThread(CountDownLatch latch) { this.latch = latch; } @Override public void run() { System.out.println("Thread running with thread name "+Thread.currentThread().getName()); System.out.println("Thread execution completed"); System.out.println("*************************"); latch.countDown(); } } public class App { public static void main(String[] args) { CountDownLatch latch = new CountDownLatch(4); SomeThread thread1 = new SomeThread(latch); SomeThread thread2 = new SomeThread(latch); SomeThread thread3 = new SomeThread(latch); SomeThread thread4 = new SomeThread(latch); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.out.println("I am in main thread"); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
I am in main thread Thread running with thread name Thread-0 Thread execution completed ************************* Thread running with thread name Thread-2 Thread execution completed ************************* Thread running with thread name Thread-1 Thread execution completed ************************* Thread running with thread name Thread-3 Thread execution completed ************************* |
The following program demonstrates how to use CountDownLatch, It’s used when a thread needs to wait for other threads before starting its work.
As we have seen above program, where we create a task that is going to wait for four threads before it starts. Then we create four threads and start them.
Here we call the latch.await() method. This method will force the main thread to wait for four threads until the lock is released.
In order to release the lock, we use latch.countDown()Â method. It reduces the count of the latch and released all waiting threads.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class SomeThread extends Thread{ private CountDownLatch latch; public SomeThread(CountDownLatch latch) { this.latch = latch; } @Override public void run() { System.out.println("Thread running with thread name "+Thread.currentThread().getName()); System.out.println("Thread execution completed"); System.out.println("*************************"); latch.countDown(); } } public class App { public static void main(String[] args) { CountDownLatch latch = new CountDownLatch(4); SomeThread thread1 = new SomeThread(latch); SomeThread thread2 = new SomeThread(latch); SomeThread thread3 = new SomeThread(latch); SomeThread thread4 = new SomeThread(latch); thread1.start(); thread2.start(); thread3.start(); thread4.start(); try { latch.await(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("I am in main thread"); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Thread running with thread name Thread-0 Thread execution completed ************************* Thread running with thread name Thread-2 Thread execution completed ************************* Thread running with thread name Thread-3 Thread execution completed ************************* Thread running with thread name Thread-1 Thread execution completed ************************* I am in main thread |
Here is the same example which demonstrates if the counter variable is more than 4 and we call latch.countDown() method 4 times. Then we won’t be able to terminate the program and can’t execute the main thread.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class SomeThread extends Thread{ private CountDownLatch latch; public SomeThread(CountDownLatch latch) { this.latch = latch; } @Override public void run() { System.out.println("Thread running with thread name "+Thread.currentThread().getName()); System.out.println("Thread execution completed"); System.out.println("*************************"); latch.countDown(); } } public class App { public static void main(String[] args) { CountDownLatch latch = new CountDownLatch(5); SomeThread thread1 = new SomeThread(latch); SomeThread thread2 = new SomeThread(latch); SomeThread thread3 = new SomeThread(latch); SomeThread thread4 = new SomeThread(latch); thread1.start(); thread2.start(); thread3.start(); thread4.start(); try { latch.await(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("I am in main thread"); } } |
Output
Thread running with thread name Thread-0
Thread execution completed
*************************
Thread running with thread name Thread-2
Thread execution completed
*************************
Thread running with thread name Thread-3
Thread execution completed
*************************
Thread running with thread name Thread-1
Thread execution completed
*************************
Contributed by: Poonam Tomar