In Fork/Join example in java I transformed the elements from a list using fork/join framework. The elements were split in half and processed in parallel until the chunk was small enough to be processed sequentially. This work of partitioning and then traversing the elements of a source is what Spliterator is doing for a living. The first step was to change ForkJoinConverter into ForkJoinSpliteratorConverter to use a spliterator. Relevant differences are in compute() and computeSequentially() methods that changed from:


By using default Spliterator created by List.spliterator() method, the code actually increased in complexity so the next step was to implement my own spliterator:

The logic that checks the THRESHOLD before splitting is handled here so now the new fork/join converter is a little bit simpler:

Also the same spliterator code can be used with streams:

In a future post, I will check the performance of this spliterator so stay tuned.

Your thoughts are welcome

This site uses Akismet to reduce spam. Learn how your comment data is processed.