title: CountDownLatch - understanding await and countDown
body: <p>According to Javadoc:
  A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero.</p>
  
  <p>That means in the below code, since I initialized the CountDownLatch to 1. All threads should get unblocked from its await method as soon as the latch calls countdown. </p>
  
  <p>But, the main thread is waiting for all the threads to complete. And also, I didn't join the main thread to end of the other threads. Why is the main thread waiting?</p>
  
  <pre><code>    import java.util.concurrent.CountDownLatch;
      import java.util.concurrent.atomic.AtomicLong;
  
  public class Sample implements Runnable {
  
      private CountDownLatch latch;
  
      public Sample(CountDownLatch latch)
      {
          this.latch = latch;
      }
      private static AtomicLong number = new AtomicLong(0);
  
      public long next() {
           return number.getAndIncrement();
      }
  
      public static void main(String[] args) {
  
          CountDownLatch latch = new CountDownLatch(1);
          for (int threadNo = 0; threadNo &lt; 4000; threadNo++) {
            Runnable t = new Sample(latch);
            new Thread(t).start();
          }
          try {
              latch.countDown();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void run() {
          try {
              latch.await();
              Thread.sleep(100);
              System.out.println("Count:"+next());
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>