Java Thread How to - Use await and countDown from CountDownLatch








Question

We would like to know how to use await and countDown from CountDownLatch.

Answer

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
// ww w  .j a  v a 2  s.co  m
public class Main {
  public static void main(String[] args) {
    CountDownLatch latch = new CountDownLatch(1);
    for (int threadNo = 0; threadNo < 1000; threadNo++) {
      Runnable t = new Test(latch);
      new Thread(t).start();
    }
    try {
      latch.await();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("done");
  }
}
class Test implements Runnable {
  CountDownLatch latch;
  public Test(CountDownLatch latch) {
    this.latch = latch;
  }
  private static AtomicLong number = new AtomicLong(0);
  public long next() {
    return number.getAndIncrement();
  }
  @Override
  public void run() {
    try {
      Thread.sleep(1000 + (int) (Math.random() * 3000));
      System.out.println(next());
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      latch.countDown();
    }
  }
}

The code above generates the following result.