Java Reference
In-Depth Information
Finally, there will be a BlockingQueueSpliterator for each operation to be performed on the
stream. Each BlockingQueueSpliterator will have a reference to one of the BlockingQueues
populated by the ForkingStreamConsumer, and it can be implemented as shown in the
following listing.
Listing C.5. A Spliterator reading the elements it traverses from a
BlockingQueue
class BlockingQueueSpliterator<T> implements Spliterator<T> {
private final BlockingQueue<T> q;
BlockingQueueSpliterator(BlockingQueue<T> q) {
this.q = q;
}
@Override
public boolean tryAdvance(Consumer<? super T> action) {
T t;
while (true) {
try {
t = q.take();
break;
} catch (InterruptedException e) { }
}
if (t != ForkingStreamConsumer.END_OF_STREAM) {
action.accept(t);
return true;
}
return false;
}
@Override
public Spliterator<T> trySplit() {
return null;
}
 
Search WWH ::




Custom Search