1.

How Countdownlatch Work In Java?

Answer»

Any thread, USUALLY main thread of application, which calls CountDownLatch.await() will wait until count REACHES zero or its interrupted by another Thread. All other thread are required to do count down by calling CountDownLatch.countDown() once they are completed or ready to the job. as soon as count reaches zero, Thread awaiting starts running. One of the disadvantage of CountDownLatch is that its not reusable once count reaches to zero

you can not use CountDownLatch any more, but don't worry Java concurrency API has another concurrent UTILITY called CyclicBarrier for such requirements Java program requires 3 services namely CacheService, AlertService and ValidationService to be started and ready before application can handle any request and this is achieved by 

using CountDownLatch in Java.

import java.util.Date;

import java.util.concurrent.CountDownLatch;

import java.util.logging.Level;

import java.util.logging.Logger;

public class CountDownLatchDemo {

 public static void main(String args[]) {

final CountDownLatch latch = new CountDownLatch(3);

Thread cacheService = new Thread(new Service("CacheService", 1000, latch));

Thread alertService = new Thread(new Service("AlertService", 1000, latch));

Thread validationService = new Thread(new Service("ValidationService", 1000, latch));

cacheService.start(); //separate thread will initialize CacheService

alertService.start(); //another thread for AlertService initialization

validationService.start();

// application should not start processing any thread until all service is up

// and ready to do there job.main thread will start with count 3

// and wait until count reaches zero. Each thread once up and read will do

// a count down.

//count is 3 since we have 3 Threads

try{

latch.await(); //main thread is waiting on CountDownLatch to finish

System.out.println("All services are up, Application is starting now");

}catch(InterruptedException ie){

ie.printStackTrace();

}

}

}

/**

 * Class executed by Thread using CountDownLatch synchronizer.

 */

class Service implements Runnable{

private final String name;

private final int timeToStart;

private final CountDownLatch latch;

public Service(String name, int timeToStart, CountDownLatch latch){

this.name = name;

this.timeToStart = timeToStart;

this.latch = latch;

}

@Override

public void run() {

try {

Thread.sleep(timeToStart);

} catch (InterruptedException EX) {

Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);

}

System.out.println( name + " is Up");

latch.countDown(); //reduce count of CountDownLatch by 1

}

 }

Output of Program:

ValidationService is Up

AlertService is Up

CacheService is Up

All services are up, Application is starting now

Points To Remember

  1. You can not reuse CountDownLatch once count is reaches to zero, this is the main difference between CountDownLatch and CyclicBarrier.
  2. Main Thread wait on Latch by calling CountDownLatch.await() method while other thread calls CountDownLatch.countDown() to INFORM that they have completed.

Any thread, usually main thread of application, which calls CountDownLatch.await() will wait until count reaches zero or its interrupted by another Thread. All other thread are required to do count down by calling CountDownLatch.countDown() once they are completed or ready to the job. as soon as count reaches zero, Thread awaiting starts running. One of the disadvantage of CountDownLatch is that its not reusable once count reaches to zero

you can not use CountDownLatch any more, but don't worry Java concurrency API has another concurrent utility called CyclicBarrier for such requirements Java program requires 3 services namely CacheService, AlertService and ValidationService to be started and ready before application can handle any request and this is achieved by 

using CountDownLatch in Java.

import java.util.Date;

import java.util.concurrent.CountDownLatch;

import java.util.logging.Level;

import java.util.logging.Logger;

public class CountDownLatchDemo {

 public static void main(String args[]) {

final CountDownLatch latch = new CountDownLatch(3);

Thread cacheService = new Thread(new Service("CacheService", 1000, latch));

Thread alertService = new Thread(new Service("AlertService", 1000, latch));

Thread validationService = new Thread(new Service("ValidationService", 1000, latch));

cacheService.start(); //separate thread will initialize CacheService

alertService.start(); //another thread for AlertService initialization

validationService.start();

// application should not start processing any thread until all service is up

// and ready to do there job.main thread will start with count 3

// and wait until count reaches zero. Each thread once up and read will do

// a count down.

//count is 3 since we have 3 Threads

try{

latch.await(); //main thread is waiting on CountDownLatch to finish

System.out.println("All services are up, Application is starting now");

}catch(InterruptedException ie){

ie.printStackTrace();

}

}

}

/**

 * Class executed by Thread using CountDownLatch synchronizer.

 */

class Service implements Runnable{

private final String name;

private final int timeToStart;

private final CountDownLatch latch;

public Service(String name, int timeToStart, CountDownLatch latch){

this.name = name;

this.timeToStart = timeToStart;

this.latch = latch;

}

@Override

public void run() {

try {

Thread.sleep(timeToStart);

} catch (InterruptedException ex) {

Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);

}

System.out.println( name + " is Up");

latch.countDown(); //reduce count of CountDownLatch by 1

}

 }

Output of Program:

ValidationService is Up

AlertService is Up

CacheService is Up

All services are up, Application is starting now

Points To Remember



Discussion

No Comment Found