gpt4 book ai didi

Java Stream API - 并行和顺序流

转载 作者:行者123 更新时间:2023-12-01 09:56:38 25 4
gpt4 key购买 nike

我是 Stream API 的新手。我有一个关于 Stream API 的问题,特别是并行和顺序流。问题是:例如,如果我有这样的伪代码:

someStream
.parallel()
.filter(some_predicate)
.sequential()
.map(some_mapping_function)
.terminal_operation();
Stream API 是要并行执行过滤器并按顺序映射,还是仅更改流的“并行特性”,因此,在此示例中,流将是顺序的?

最佳答案

调用 sequential()parallel()只需更改整个流的模式。调用终端操作时流处于哪种模式是使用的模式。在你的情况下,因为 sequential()是在 parellel() 之后您的流将按顺序执行。
这是由 Stream 记录的界面:

Stream pipelines may execute either sequentially or in parallel. This execution mode is a property of the stream [emphasis added]. Streams are created with an initial choice of sequential or parallel execution. (For example, Collection.stream() creates a sequential stream, and Collection.parallelStream() creates a parallel one.) This choice of execution mode may be modified by the BaseStream.sequential() or BaseStream.parallel() methods, and may be queried with the BaseStream.isParallel() method.


java.util.stream 包裹:

The stream pipeline is executed sequentially or in parallel depending on the mode of the stream on which the terminal operation is invoked. The sequential or parallel mode of a stream can be determined with the BaseStream.isParallel() method, and the stream's mode can be modified with the BaseStream.sequential() and BaseStream.parallel() operations. The most recent sequential or parallel mode setting applies to the execution of the entire stream pipeline [emphasis added].

关于Java Stream API - 并行和顺序流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63112716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com