Friday, June 21, 2013

Use of CountDownLatch API in java

This is a brand new API introduced in Java 5. This API is dealing with synchronization. It means that at any given point of time only one thread can access the block of code.

This API is under the java.util.concurrent package.

This is best suitable where you want to initialized all the sub programs before completing or other work.

Suppose, we have a program, which has LogManager and ErrorManager objects. At the same time we want to initialize all these objects then only the main program can do other work.

The CountDownLatch API has the following methods:

  1. await() - Causes the current thread to wait until the latch has counted down to zero.
  2. await(long timeout, TimeUnit unit) - Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted, or the specified waiting time elapses.
  3. countDown() - Decrements the count of the latch, releasing all waiting threads if the count reaches zero.
  4. getCount() - Returns the current count.







1 comment: