Fork/join and parallel streams question 108 of 180 senior
What is a Spliterator and why does it matter for parallelism?
- An iterator that can be reset
- Correct: The stream source: it traverses and splits, so how well it splits decides whether parallelism helps
- A collector for splitting results
- A JIT optimisation
Every stream is backed by a Spliterator. Sequentially it behaves like an iterator via
tryAdvance/forEachRemaining; in parallel the framework calls trySplit recursively to build the
task tree.
Its characteristics (SIZED, SUBSIZED, ORDERED, DISTINCT, SORTED, IMMUTABLE, NONNULL) are
what the pipeline optimises against. An array spliterator is SIZED and SUBSIZED, so splits are
exact and free. A linked list's reports the same flags, but
its trySplit has to walk the nodes and copy a batch into an array, and the batches start small and
grow, so every split costs a traversal and the halves are uneven.
If you write a custom data source and want parallel streams over it to be worth anything, implementing
trySplit and reporting accurate characteristics is the work. The default from
Spliterators.spliteratorUnknownSize splits poorly on purpose.
Quiz yourself on Fork/join and parallel streams Suggest a correction