Java 8 `Stream` 可以在您不要求的情况下实现并行吗? [英] Can a Java 8 `Stream` be parallel without you even asking for it?

查看:34
本文介绍了Java 8 `Stream` 可以在您不要求的情况下实现并行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如我所见,显而易见的代码,在使用 Java 8 Stream 时,无论是对象"流还是原始流(即 IntStream 和朋友)) 将只是使用:

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) 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` 可以在您不要求的情况下实现并行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆