Java Reference
In-Depth Information
Figure 7.5. The work-stealing algorithm used by the fork/join
framework
It should now be clear how a stream can use the fork/join framework to process its items in
parallel, but there's still one missing ingredient. In this section, we analyzed an example where
you explicitly developed the logic to split an array of numbers into multiple tasks. Nevertheless,
you didn't have to do anything similar when you used the parallel streams at the beginning of
this chapter, and this means that there must be an automatic mechanism splitting the stream for
you. This new automatic mechanism is called the Spliterator, and we explore it in the next
section.
7.3. Spliterator
The Spliterator is another new interface added to Java 8; its name stands for “splitable iterator.”
Like Iterators, Spliterators are used to traverse the elements of a source, but they're also
designed to do this in parallel. Although you may not have to develop your own Spliterator in
practice, understanding how to do so will give you a wider understanding about how parallel
streams work. Java 8 already provides a default Spliterator implementation for all the data
structures included in its Collections Framework. Collections now implements the interface
Spliterator, which provides a method spliterator. This interface defines several methods, as
shown in the following listing.
 
Search WWH ::




Custom Search