In this post, We will talk and learn about CountDownLatch in Java using an Example.
CountDownLatch is a synchronization helping class that allows one or more threads to wait until a set of operations being performed in other threads completes.
A CountDownLatch has to initialize with a given count. The await method blocks until and unless the current count reaches to zero due to invocations of the countDown() method every time, after that all waiting threads are released and any subsequent invocations of await return immediately.
This is a one-shot phenomenon that in CountDownLatch the count cannot be reset. If you need a version that resets the count then consider using a CyclicBarrier.
The CountDownLatch initialized with count N at the time of instantiation and it can be used to make one thread wait until N threads have completed some action or some action has been completed N number of times.
Complete source code:
DevTeam.java
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 |
package com.infotech.tasks; import java.util.concurrent.CountDownLatch; public class DevTeam extends Thread { private CountDownLatch countDownLatch; public DevTeam(CountDownLatch countDownLatch, String name) { super(name); this.countDownLatch = countDownLatch; } @Override public void run() { System.out.println("Task assigned to development team " +Thread.currentThread().getName()); try { Thread.sleep(3000); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.println("Task finished by development team "+Thread.currentThread().getName()); //Each thread calls countDown() method on task completion. countDownLatch.countDown(); } } |
QATeam.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.infotech.tasks; public class QATeam extends Thread { public QATeam(String name) { super(name); } @Override public void run() { System.out.println("Task assigned to "+Thread.currentThread().getName()); try { Thread.sleep(2000); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.println("Task finished by "+Thread.currentThread().getName()); } } |
AssignTaskManagerTest.Java
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 |
package com.infotech.client; import java.util.concurrent.CountDownLatch; import com.infotech.tasks.DevTeam; import com.infotech.tasks.QATeam; public class AssignTaskManagerTest { public static void main(String[] args) throws InterruptedException { //Created CountDownLatch for 2 threads CountDownLatch countDownLatch = new CountDownLatch(2); //Created and started two threads DevTeam teamDevA = new DevTeam(countDownLatch, "dev-A"); DevTeam teamDevB = new DevTeam(countDownLatch, "dev-B"); teamDevA.start(); teamDevB.start(); //When two threads(dev-A and dev-B)completed tasks are returned countDownLatch.await(); //Now execution of thread(QA team) started.. QATeam qaTeam = new QATeam("QA team"); qaTeam.start(); } } |
Output of this Program:
Task assigned to development team dev-A
Task assigned to development team dev-B
Task finished by development team dev-A
Task finished by development team dev-B
Task assigned to QA team
Task finished by QA team
You May Also Like:
How would you round a double value to certain decimal Precision and Scale ?
What is BlockingQueue? How can we implement Producer and Consumer problem using Blocking Queue?
How to get number of available processors in Java ?
That’s all about CountDownLatch Example in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.