Java 8`Stream`可以并行,你甚至不需要它吗? [英] Can a Java 8 `Stream` be parallel without you even asking for it?

查看:266
本文介绍了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天全站免登陆