title: Is this an Improper use of CountdownLatch with initial count = 1
body: <p>Below is the snippet, listing 5.11 from book 'java concurrency in practice' by Brain Goetz.</p>
  
  <p>I find the below code confusing. It seems that countdownlatch 'startGate' has a flawed usage. ( 'endGate' usage is OK )</p>
  
  <pre><code>public class TestHarness {
  public long timeTasks(int nThreads, final Runnable task)
          throws InterruptedException {
      final CountDownLatch startGate = new CountDownLatch(1);
      final CountDownLatch endGate = new CountDownLatch(nThreads);
      for (int i = 0; i &lt; nThreads; i++) {
          Thread t = new Thread() {
              public void run() {
                  try {
                      startGate.await();
                      try {
                          task.run();
                      } finally {
                          endGate.countDown();
                      }
                  } catch (InterruptedException ignored) {
                  }
              }
          };
          t.start();
      }
      long start = System.nanoTime();
      startGate.countDown();
      endGate.await();
      long end = System.nanoTime();
      return end - start;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Execution of line <code>startGate.countDown()</code> will notify all the worker threads waiting on the latch. But is this guaranteed to notify all the nThreads ?<br/>
  It is quite possible only a few threads, say 2 threads, out nTheads have executed startGate.await() call of the run() method. <br/>
  And at this time the main thread executes <code>startGate.countDown()</code>. This call will notify only the 2 threads ( out of nThreads ) waiting on the latch. <br/></p>
  
  <p>Now the rest of the other threads ( nThread - 2 ) will call <code>startGate.await()</code> later in time, but as the main thread has already raised a notification, so the worker threads( nThreads- 2) will now wait indefinitely for notification ?</p>
  
  <p>Or I am missing something ?</p>