Introduction To CountDownLatch In Java

Posted By : Prashank Jauhari | 22-Jun-2018

Count Down latch is thread utility class provided by java which provides the facility through which one or more thread waits for a finite number of thread(All thread running parallelly). This facility is highly useful when we need that certain set of services should be started before the application can be started.

 

How to Create CountDownLatch

Step-1) Create an Object of CountDownLatch with an integer value(number of  threads to wait for).

CountDownLatch latch=new CountDownLatch(2);

 

step2)Now threads that need to wait for other thread will call latch.await().

 

step3)When threads in the finite set call latch.countDown()  counter value gets decremented by 1 and when the counter reaches zero all waiting thread resumes.

 

Following is the dummy programme for CountDownLatch that will explain the working in a simple way and will bring all the component together.

 

 

/**
 * A sample programme to demonstrate the 
 * use of count down latch is java.
 * @author prashank
 *
 */
public class CountDownLatch {

	private static java.util.concurrent.CountDownLatch counter=new java.util.concurrent.CountDownLatch(2);
	public static void main(String[] args){
		//this line will get the initial value of counter
		System.out.println("initial value of counter : "+counter.getCount());
		
		Thread marketRateService=new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("initializing market rates,wait for four seconds.\n");
					Thread.sleep(4000);
					counter.countDown();
					System.out.println("market rate initilize.\n");
				} catch (InterruptedException e){
					System.out.println("Market rate service gets intrupted.\n");
				}
			}
		}, "marketRateService");
		Thread coinInitService=new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("initializing coins,wait for 1 seconds.\n");
					Thread.sleep(1000);
					counter.countDown();
					System.out.println("All coins nodes successfully started.\n");
				} catch (InterruptedException e){
					System.out.println("Market coin init service intrupted.\n");
				}
			}
		}, "coinInitService");
		
		System.out.println("Both threads are running parallely and will decrement the counter once they done.\n");
		marketRateService.start();
		coinInitService.start();
		try {
			System.out.println("Main Thread is waiting to reach countDownLatch counter to reach 0.\n");
			counter.await();
			System.out.println("Market initilize successfully and coin nodes started  completed successfully.\n");
			System.out.println("starting Exchange server.\n");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
}

 

Hope Above code is helpful for someone.

 

 

 

About Author

Author Image
Prashank Jauhari

Prashank is quick learner and passionate about new technologies. He is currently working as Java Developer with knowledge of Spring and Hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..