在流管道中放置 parallel() 调用的位置重要吗?
Does it matter where to place parallel() call in the stream pipeline?
例如,parallel()
在 filter()
之前
Stream.of(...)
.parallel()
.filter(...)
.forEach(...);
在 filter()
之后 和 parallel()
Stream.of(...)
.filter(...)
.parallel()
.forEach(...);
调用顺序 parallel()
会影响流吗?
放在哪里都没有关系 parallel()
。在调用终端操作(在您的情况下 forEach
)之前,不会评估流。
来自 Javadoc:
Returns an equivalent stream that is parallel. May return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel.
This is an intermediate operation.
唯一重要的情况是您同时调用 sequential
,在这种情况下,以下情况适用:
The most recent sequential or parallel mode setting applies to the execution of the entire stream pipeline.
有关详细信息,请参阅 the relevant Javadoc
例如,parallel()
在 filter()
Stream.of(...)
.parallel()
.filter(...)
.forEach(...);
在 filter()
之后 和 parallel()
Stream.of(...)
.filter(...)
.parallel()
.forEach(...);
调用顺序 parallel()
会影响流吗?
放在哪里都没有关系 parallel()
。在调用终端操作(在您的情况下 forEach
)之前,不会评估流。
来自 Javadoc:
Returns an equivalent stream that is parallel. May return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel. This is an intermediate operation.
唯一重要的情况是您同时调用 sequential
,在这种情况下,以下情况适用:
The most recent sequential or parallel mode setting applies to the execution of the entire stream pipeline.
有关详细信息,请参阅 the relevant Javadoc