When reading documentation of the Java 8 Spliterator I encountered the notion of "Serial thread confinement". Preciselly the documenatation says:
Despite their obvious utility in parallel algorithms, spliterators are not expected to be thread-safe; instead, implementations of parallel algorithms using spliterators should ensure that the spliterator is only used by one thread at a time. This is generally easy to attain via serial thread-confinement, which often is a natural consequence of typical parallel algorithms that work by recursive decomposition
Q1: By the first sentence above, I assume that when writing implementations of Spliterator
I should return new instance of Spliterator which will operate on isolated subset of traversed data (so that the subset of data can not be accessed by other threads). Is that right?
Q2: What does the second sentence mean. What is this "serial thread-confinment" and why is it "natural consequence of typical parallel algorithms"?
Q1: That is correct, only one thread should own a Splititerator at one time. When using for instance on a Splititerator the trySplit method will return (if the collection can be split) a new Splititerator you should give to only one thread (another thread should handle the original Splititerator which now contains only the element that were not removed by the trySplit call)
Q2: Serial thread confinement means that you can safely publish a splititerator from one thread to another, as long as the splititerator is confined to a single thread
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With