title: How to make a concurrent iterator over some source?
body: <p>I would like to have an iterator that can be read by multiple threads concurrently so that I can process the data of the iterator's source in parallel. The challenge is that I can't really couple <code>hasNext()</code> with its logical <code>next()</code> as those could go to different threads. (That is, two threads can call <code>hasNext()</code>, each see true, and then have the second thread fail because there was only one item.) My problem is that for some sources I don't really know if it has a next element until I try to read it. One such example is reading lines from a file; another is reading <code>Term</code> instances from a Lucene index. </p>
  
  <p>I was thinking of setting up a queue inside the iterator and feeding the queue with a separate thread. That way, <code>hasNext()</code> is implemented in terms of the queue size. But I don't see how I could guarantee that the queue is filled because that thread could get starved.</p>
  
  <p>Should I ignore the Iterator contract, and just call <code>next()</code> exhaustively until a <code>NoSuchElementException</code> is thrown?</p>
  
  <p>Is there a more elegant way of handling the problem? </p>