Java CountDownLatch.await(long timeout, TimeUnit unit)

Syntax

CountDownLatch.await(long timeout, TimeUnit unit) has the following syntax.

public boolean await(long timeout,  TimeUnit unit)  throws InterruptedException

Example

In the following code shows how to use CountDownLatch.await(long timeout, TimeUnit unit) method.


//from w  ww.  j av  a 2  s. c  om
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
   
public class Main {
  public static void main(String args[]) {
    CountDownLatch cdl = new CountDownLatch(5);
    System.out.println(cdl.getCount());
    new MyThread(cdl);
   
    try {
      cdl.await(100,TimeUnit.SECONDS);
    } catch (InterruptedException exc) {
      System.out.println(exc);
    }
    System.out.println("Done");
  }
}
   
class MyThread implements Runnable {
  CountDownLatch latch;
   
  MyThread(CountDownLatch c) {
    latch = c;
    new Thread(this).start();
  }
   
  public void run() {
    for(int i = 0; i<5; i++) {
      System.out.println(i);
      latch.countDown(); // decrement count
    }
  }
}




















Home »
  Java Tutorial »
    java.util.concurrent »




ArrayBlockingQueue
BlockingDeque
CountDownLatch
CyclicBarrier
TimeUnit