Java Reference
In-Depth Information
Listing C.4. A ForkingStreamConsumer to add stream elements to
multiple queues
This class implements both the Consumer and Results interfaces and holds a reference to the
List of BlockingQueues and to the Map of Futures executing the different operations on the
stream.
The Consumer interface requires an implementation for the method accept. Here, every time
ForkingStreamConsumer accepts an element of the stream, it adds that element to all the
BlockingQueues. Also, after all the elements of the original stream have been added to all queues,
the finish method causes one last item to be added to all of them. This item, when met by
BlockingQueueSpliterators, will make the queues understand that there are no more elements to
be processed.
The Results interface requires an implementation for the get method. Here, it retrieves the
Future that's indexed in the Map with the argument key and unwraps its result or waits until a
result is available.
 
Search WWH ::




Custom Search