Can a Java 8 `Stream` be parallel without you even asking for it?

前端 未结 5 1035
借酒劲吻你
借酒劲吻你 2021-01-01 15:35

As I see it, the obvious code, when using Java 8 Streams, whether they be \"object\" streams or primitive streams (that is, IntStream and friends)

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 16:00

    It is mentioned here: "When you create a stream, it is always a serial stream unless otherwise specified." And here: "It is allowable for this method (parallelStream) to return a sequential stream."

    CONCURRENT and IMMUTABLE aren't (directly) related to this. They specify whether the underlying collection can be modified without rendering the spliterator invalid or whether it is immutable respectively. The feature of spliterator that does pretty much define the behavior of parallelStream is trySplit. Terminal operations on a parallel stream will eventually invoke trySplit, and whatever that implementation does will in the end of the day define what parts, if any, of the data are processed in parallel.

提交回复
热议问题