In this thread concurrency tutorial we will learn what is java.util.concurrent.CountDownLatch in java with program and examples. Programs to demonstrate usage of CountDownLatch in thread concurrency in java.
Contents of page :
- 1) What is CountDownLatch in java ?
- 1.1) CountDownLatch’s constructor in java >
- CountDownLatch(int count)
- 1.2) CountDownLatch’s await() method has 2 forms :
- void await( ) throws InterruptedException
- boolean await(long timeout, TimeUnit unit)
- 1.3) CountDownLatch’s countDown() method in java :
- 1.4) In short about CountDownLatch’s await() and countDown() method in java >
- 2) Program to demonstrate usage of CountDownLatch >
- 2.1) Let’s discuss output in detail, to get better understanding of CountDownLatch usage in program in java >
- 2.2) Occasionally, because of threads unpredictable behaviour output may be bit awkward in java >
- 3) Application of CountDownLatch in real world >
1) What is CountDownLatch in java?
There might be situation where we might like our thread to wait until one or more threads completes certain operation.
A CountDownLatch is initialized with a given count .
count specifies the number of events that must occur before latch is released.
Every time a event happens count is reduced by 1. Once count reaches 0 latch is released.
1.1) CountDownLatch’s constructor in java >
- CountDownLatch(int count)
CountDownLatch is initialized with given count.
count specifies the number of events that must occur before latch is released.
1.2) CountDownLatch’s await() method has 2 forms in java :
- void await( ) throws InterruptedException
Causes the current thread to wait until one of the following things happens-
- latch count has down to reached 0, or
- unless the thread is interrupted.
- boolean await(long timeout, TimeUnit unit)
Causes the current thread to wait until one of the following things happens-
- latch count has down to reached 0,
- unless the thread is interrupted, or
- specified timeout elapses.
1.3) CountDownLatch’s countDown() method in java :
- void countDown( )
Reduces latch count by 1.
If count reaches 0, all waiting threads are released.
1.4) In short about CountDownLatch’s await() and countDown() method in java >
The await() methods block the current threads until the count reaches 0 due to invocations of the countDown() method by some other thread, after which blocked threads are released.
2) Program to demonstrate usage of CountDownLatch in java >
import java.util.concurrent.CountDownLatch;
class MyRunnable implements Runnable{
CountDownLatch countDownLatch;
MyRunnable(CountDownLatch countDownLatch){
this.countDownLatch=countDownLatch;
}
public void run(){
for(int i=2;i>=0;i--){
countDownLatch.countDown();
System.out.println(Thread.currentThread().getName()+
" has reduced latch count to : "+ i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class CountDownLatchTest {
public static void main(String[] args) {
CountDownLatch countDownLatch=new CountDownLatch(3);
System.out.println("CountDownLatch has been created with count=3");
new Thread(new MyRunnable(countDownLatch),"Thread-1").start();
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("count has reached zero, "+
Thread.currentThread().getName()+" thread has ended");
}
}
/*OUTPUT
CountDownLatch has been created with count=3
Thread-1 has reduced latch count to : 2
Thread-1 has reduced latch count to : 1
Thread-1 has reduced latch count to : 0
count has reached zero, main thread has ended
*/
|
2.1) Let’s discuss output in detail, to get better understanding of CountDownLatch usage in program in java >
Note : I have mentioned output in green text.
CountDownLatch has been created with count=3
Initially, CountDownLatch is created with count=3
Initially, CountDownLatch is created with count=3
main thread called countDownLatch.await() and it is waiting for count to become 0.
Thread-1 called countDownLatch.countDown() method. [Now, count=2]
Thread-1 has reduced latch count to : 2
Thread-1 called countDownLatch.countDown() method. [Now, count=1]
Thread-1 has reduced latch count to : 1
Thread-1 called countDownLatch.countDown() method. [Now, count=0]
Thread-1 has reduced latch count to : 0
count has reached zero, main thread has ended
As, count has reached zero, main thread has ended.
2.2) Occasionally, because of threads unpredictable behaviour output may be bit awkward in java >
/*OUTPUT
CountDownLatch has been created with count=3
Thread-1 has reduced latch count to : 2
Thread-1 has reduced latch count to : 1
count has reached zero, main thread has ended
Thread-1 has reduced latch count to : 0
*/
This may happen because as soon as count reaches 0 waiting threads are released. Here, as soon as Thread-1 called countDown() method third time main thread was released and its sysout statement was executed before Thread-1’s sysout statement.
3) Application of CountDownLatch in real world >
When you go in amusement park, you must have seen on certain rides there is mandate that at least 3 people (3 is count) should be there to take a ride. So, ride keeper (ride keeper is main thread) waits for 3 persons (ride keeper has called await()).
Every time a person comes count is reduced by 1 (let’s say every person is calling countDown() method). Ultimately when 3 persons reach count becomes 0 & wait for ride keeper comes to end.
SUMMARY>
In this thread concurrency tutorial we learned what is java.util.concurrent.CountDownLatch in java with program and examples. We also wrote programs to demonstrate usage of CountDownLatch in thread concurrency in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
Implementation of custom/own CountDownLatch in java
CyclicBarrier >
CyclicBarrier in java
Implementation of custom/own CyclicBarrier in java
Important Similarity and Differences >
Differences between execute() and submit() method of executor framework
Difference between synchronized and ReentrantLock
Similarity and Difference between CyclicBarrier and CountDownLatch in Java
Thread concurrency Interviews >