Concurrency

What is CountDownLatch in Java

Posted on Updated on

What is CountDownLatch in Java?

CountDownLatch is part of java.util.concurrent package which allow one or more threads or main Thread will wait until set of operations get accomplished.
CountDownLatch works on latching or triggering concept over set of threads, it’s coordinate among running Thread by signaling.
A CountDownLatch is initialized with a given counter/latch and the await() method will block until counter/latch reach to zero. Every invocation of countdown() method, counter/latch value is decrement by one.

Useful Methods :

Await() : It basically block current Thread until latch value reach to zero.
countdown() : It decrement latch value by one and releasing all blocking Thread unless latch has count down to zero.

How to initialized CountDownLatch?

Using public constructor it is initialized CountDownLatch with given latch size.

Example:-

CountDownLatch countDownLatch = new CountDownLatch(10);
Here latch size is 10 which means Main Thread will be block until 10 latch has count down to zero. If latch has count down to zero then Main Thread start execution.

Where we Use CountDownLatch

  •  Server Startup : we usually see when we start Server either web/application Server a lot of services initialized in console or log file before Server start up. Here we can see Server Startup Program is block until rest or dependency services are initialized.
  •  Second, suppose as per our work/project requirement we want to do some analysis on operation data that are being fetched from different databases or source system.

Snippet Code here :-

Below here, In this example code, we describe how ApplicationStartUp class is wait for turn until all their services are completed.

AbstractService.Java : It’s a base service class which has three Child classes given below that give the implementation of  abstract method service().

  • CachingService.Java
  • InMemoryService.Java
  • UIService.Java

ApplicationStartUp.Java : It’s is main program to execute all services before Server startup.

AbstractService.Java

package com.app.service;

import java.util.concurrent.CountDownLatch;

public abstract class AbstractService {

	private CountDownLatch countDownLatch;

	public AbstractService(CountDownLatch latch) {
		this.countDownLatch = latch;
	}

	public void startup() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					serviceUp();
				} catch (Exception e) {
				} finally {
					countDownLatch.countDown();
				}
			}

		}).start();
		;
	}

	public abstract void serviceUp();

}


 

CachingService.Java


package com.app.service;

import java.util.concurrent.CountDownLatch;

public class CachingService extends AbstractService {

	public CachingService(CountDownLatch latch){
		super(latch);
	}
	
	@Override
	public void serviceUp() {
		System.out.println(" CachingServer Startup complated");
		
	}

}


 

InMemoryService.Java


package com.app.service;

import java.util.concurrent.CountDownLatch;

public class InMemoryService extends AbstractService {

	public InMemoryService(CountDownLatch latch){
		super(latch);
	}
	
	@Override
	public void serviceUp() {
		System.out.println(" InMemoryService Startup complated");
		
	}

}


 

UIService.Java


package com.app.service;

import java.util.concurrent.CountDownLatch;

public class UIService extends AbstractService {

	public UIService(CountDownLatch latch){
		super(latch);
	}
	
	@Override
	public void serviceUp() {
		System.out.println(" UIService Startup complated");
		
	}

}


 

ApplicationStartUp.Java


package com.app.startup;

import java.util.concurrent.CountDownLatch;

import com.app.service.AbstractService;
import com.app.service.CachingService;
import com.app.service.InMemoryService;
import com.app.service.UIService;

public class ApplicationStartUp {
	private final CountDownLatch latch = new CountDownLatch(3);
	private final AbstractService [] services = {new CachingService(latch),new InMemoryService(latch),new UIService(latch)};
	
	public static void main(String args[]) throws InterruptedException{
		
		ApplicationStartUp app = new ApplicationStartUp();
		app.startServer();
		
	}
	
	public void startServer() throws InterruptedException
	{
		System.out.println(" Server Starting ..........................");
		for( AbstractService service:services){
			service.startup();
		}
		latch.await();
		System.out.println(" Server Started");
	}
	
}

I hope, you would be able to understand the concept of CountLatchDown.java and usage through the example and theory.

Thanks

Ajay Kumar

 

Advertisements