Can a Java 8 `Stream` be parallel without you even asking for it?(Java 8 `Stream` 可以在您不要求的情况下实现并行吗?)
问题描述
如我所见,显而易见的代码,在使用 Java 8 Stream
时,无论是对象"流还是原始流(即 IntStream
和朋友)) 将只是使用:
As I see it, the obvious code, when using Java 8 Stream
s, whether they be "object" streams or primitive streams (that is, IntStream
and friends) would be to just use:
someStreamableResource.stream().whatever()
但是,相当多的可流式资源"也有.parallelStream()
.
But then, quite a few "streamable resources" also have .parallelStream()
.
阅读 javadoc 时不清楚的是 .stream()
流是否始终是顺序的,以及 .parallelStream()
流是否始终是并行的...
What isn't clear when reading the javadoc is whether .stream()
streams are always sequential, and whether .parallelStream()
streams are always parallel...
然后是 Spliterator
,尤其是它的 .characteristics()
,其中之一是它可以是 CONCURRENT
,甚至 IMMUTABLE
.
And then there is Spliterator
, and in particular its .characteristics()
, one of them being that it can be CONCURRENT
, or even IMMUTABLE
.
我的直觉是,事实上,Stream
是否可以默认并行或完全并行,取决于其底层 Spliterator
...
My gut feeling is that in fact, whether a Stream
can be, or not, parallel by default, or parallel at all, is guided by its underlying Spliterator
...
我在正确的轨道上吗?我已经阅读并再次阅读了 javadocs,但仍然无法对这个问题给出明确的答案......
Am I on the right track? I have read, and read again, the javadocs, and still cannot come up with a clear answer to this question...
推荐答案
首先,通过规范的镜头.流是并行还是顺序流是流状态的一部分.流创建方法应该指定它们是创建顺序流还是并行流(JDK 中的大多数都这样做),但它们不需要这么说.如果您的流源没有说,请不要假设.如果有人传给你一条信息流,不要假设.
First, through the lens of specification. Whether a stream is parallel or sequential is part of a stream's state. Stream-creation methods should specify whether they create a sequential or parallel stream (and most in the JDK do), but they are not required to say so. If your stream source doesn't say, don't assume. If someone passes you a stream, don't assume.
允许并行流自行决定退回到顺序(因为顺序实现是一种并行实现,只是一个可能不完美的实现);反之则不然.
Parallel streams are allowed to fall back to sequential at their discretion (since a sequential implementation is a parallel implementation, just a potentially imperfect one); the opposite is not true.
现在,通过实施的镜头.在 Collections 和其他 JDK 类中的流创建方法中,我们坚持创建顺序流,除非用户明确要求并行"的原则.(但是,其他库会做出不同的选择.如果他们有礼貌,他们会指定他们的行为.)
Now, through the lens of implementation. In the stream-creation methods in Collections and other JDK classes, we stick to a discipline of "create a sequential stream unless the user explicitly asks for parallelism". (Other libraries, however, make different choices. If they're polite, they'll specify their behavior.)
流并行和Spliterator之间的关系只有一个方向.Spliterator 可以拒绝拆分——实际上是拒绝任何并行性——但它不能要求客户端拆分它.所以一个不合作的 Spliterator 可以破坏并行性,但不能确定它.
The relationship between stream parallelism and Spliterator only goes in one direction. A Spliterator can refuse to split -- effectively denying any parallelism -- but it can't demand that a client split it. So an uncooperative Spliterator can undermine parallelism, but not determine it.
这篇关于Java 8 `Stream` 可以在您不要求的情况下实现并行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 8 `Stream` 可以在您不要求的情况下实现并行吗?
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01