title: Java signal/event mechanism to indicate some value is available
body: <p>I have a generator class that owns a Thread in which a number of "records" to be generated is determined, then generates that many records (which get placed in a BlockingQueue for retrieval by another thread).</p>
  
  <p>I'd like the other thread to know how many records are going to be generated (for sensible progress reporting among other things).</p>
  
  <p>It seems Future gives me exactly the interface I'm after, but I'm new to Java, and not sure of the idiomatic way of implementing it.  </p>
  
  <p>My background is in C++/Win32, so I'd normally use a win32 "Event" (as created by <code>CreateEvent(0, true, false, 0)</code>, with <code>SetEvent</code> and <code>WaitForSingleObject</code> for my signal and wait implementations).  I've noticed Java has a <code>CountDownLatch</code>, but this somehow feels heavier than what I'm after (somewhat akin to using an int when I really want a boolean), and it seems unintuitive for this purpose (to me, anyway). </p>
  
  <p>So here's my code using CountDownLatch and a Future.  I've distilled my real code down a bit here (removed irrelevant implementation details and ignoring all error handling).</p>
  
  <pre><code>    import java.util.concurrent.BlockingQueue;
      import java.util.concurrent.CountDownLatch;
      import java.util.concurrent.Future;
      import java.util.concurrent.LinkedBlockingQueue;
      import java.util.concurrent.TimeUnit;
  
      public abstract class Generator {
  
          private CountDownLatch numRecordsSignal = new CountDownLatch(1);
  
          private int numRecords;
  
          private BlockingQueue&lt;Record&gt; queue = new LinkedBlockingQueue&lt;Record&gt;();
  
          public Generator() {
              new Thread(new Runnable() {
  
                  @Override
                  public void run() {
                      numRecords = calculateNumRecords();
                      numRecordsSignal.countDown();
  
                      for (Record r : generateRecords()) {
                          try {
                              queue.put(r);
                          } catch (InterruptedException e) {
                              // [ ... snip ... ]
                          }
                      }                
                  }
              }).start();
          }
  
          public Future&lt;Integer&gt; numRecords() {
              return new Future&lt;Integer&gt;() {
                  // Ignore cancel for now (It wouldn't make sense to cancel 
                  // just this part of the Generator's work, anyway).
                  public boolean cancel(boolean mayInterruptIfRunning) { 
                      return false; 
                  }
  
                  public Integer get() throws InterruptedException {
                      numRecordsSignal.await();
                      return numRecords;
                  }
  
                  public Integer get(long timeout, TimeUnit unit) 
                          throws InterruptedException {
                      numRecordsSignal.await(timeout, unit);
                      return numRecords;
                  }
  
                  public boolean isCancelled() {
                      return false;
                  }
  
                  public boolean isDone() {
                      // Since we can't cancel, just check the state of the 
                      // signal
                      return numRecordsSignal.getCount() == 0;
                  }
              };
          }
  
          public Record nextRecord() throws InterruptedException {
              return queue.take();
          }
  
          /** --- Boring stuff below this line --- */
          public interface Record { }
  
          protected abstract int calculateNumRecords();
  
          protected abstract Iterable&lt;Record&gt; generateRecords();                        
      }
  </code></pre>
  
  <p><strong>Now for my actual questions:</strong></p>
  
  <ol>
  <li>Is there a better mechanism than <code>CountDownLatch</code> for single-shot signalling?</li>
  <li>I want callers to be able to either wait or poll on the result, but don't need them to be able to cancel the operation.  Is Future the right way to expose this stuff?</li>
  <li>Does any of this stuff look particularly "un-Java"?  Am I on the wrong track completely?</li>
  </ol>
  
  <p><strong>Edit:</strong></p>
  
  <p>Just to clarify, I expect the caller to be able to do the following:</p>
  
  <pre><code>    Generator gen = new Generator();
      Integer numRecords = gen.numRecords().get();  // This call might block waiting for the result
      numRecords = gen.numRecords().get(); // This call will never block, as the result is already available.
  </code></pre>
  
  <p>It's just a slow-to-initialise value I'm trying to implement.  Once the "initialised" condition is met, it should latch.  The value doesn't get re-evaluated once it's known.</p>