Java Reference
In-Depth Information
In listing C.2 , you first create the List of BlockingQueues mentioned previously. Then you create
a Map, having as keys the same keys used to identify the different operations to be executed on
the stream, and having as values the Futures that will contain the corresponding results of these
operations. The List of BlockingQueues and the Map of Futures are then passed to the
constructor of the ForkingStreamConsumer. Each Future is created with this
getOperationResult method, as shown in the next listing.
Listing C.3. Future s created with the getOperationResult method
The method getOperationResult creates a new BlockingQueue and adds it to the List of queues.
This queue is passed to a new BlockingQueueSpliterator, which is a late-binding Spliterator,
reading the item to be traversed from the queue; we'll examine how it's made shortly.
You then create a sequential stream traversing this Spliterator, and finally you create a Future to
calculate the result of applying the function representing one of the operations you want to
perform on this stream. This Future is created using a static factory method of the
CompletableFuture class that implements the Future interface. This is another new class
introduced in Java 8, and we investigated it in detail in chapter 11 .
C.1.2. Developing the ForkingStreamConsumer and the
BlockingQueueSpliterator
The last two outstanding parts you need to develop are the ForkingStreamConsumer and
BlockingQueueSpliterator classes we introduced previously. The first one can be implemented
as follows.
 
Search WWH ::




Custom Search