I would like to have an iterator that can be read by multiple threads at the same time so that I can process the iterator source data in parallel. The problem is that I cannot really combine hasNext()with its logical next(), as they can go to different threads. (That is, two threads can cause hasNext(), each sees true, and then the second thread fails because there is only one element.) My problem is that for some sources I really don't know if it has the next element until I I will try to read it. One such example is reading lines from a file; another reads instances Termfrom the Lucene index.
I was thinking about setting up a queue inside an iterator and feeding the queue as a separate thread. Thus, it hasNext()is implemented in terms of queue size. But I do not understand how I can guarantee that the queue is full, because this thread can become hungry.
Should I ignore the contract with Iterator and just call next()exhaustively until it is thrown away NoSuchElementException?
Is there a more elegant way to solve the problem?
source
share