Java8的Collection.parallelStream如何工作? [英] How Java8's Collection.parallelStream works?

查看:231
本文介绍了Java8的Collection.parallelStream如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Collection类在Java SDK 8中带有新方法"parallelStream".

Collection class comes with a new method "parallelStream" in Java SDK 8.

很明显,此新方法提供了一种并行使用集合的机制.

It is obvious that this new method provides a mechanism to consume collections in parallel.

但是,我想知道Java如何实现这种并行性.潜在的机制是什么?它仅仅是多线程执行吗?还是fork/join框架(随Java SDK 7一起提供)介入了?如果没有答案,那么它如何工作?与其他两种机制相比,它的优点是什么?

But, I wonder about how Java achieve this parallelism. What is the underlying mechanism? Is it simply a multithreaded execution? Or does fork/join framework (coming with Java SDK 7) step in? If the answer is neither then, how does it work and what are the advantages of it over the other two mechanisms?

推荐答案

看看流的并行方法,您可能想知道并行流使用的线程来自何处,有多少线程以及如何自定义流程.并行流在内部使用默认的ForkJoinPool,默认情况下,ForkJoinPool的线程数与处理器的数量一样,由Runtime.getRuntime().availableProcessors()返回.但是您可以使用系统属性java.util.concurrent.ForkJoinPool.common.parallelism更改此池的大小.

Looking at the stream’s parallel method, you may wonder where the threads used by the parallel stream come from, how many there are, and how you can customize the process. Parallel streams internally use the default ForkJoinPool, which by default has as many threads as you have processors, as returned by Runtime.getRuntime().availableProcessors(). But you can change the size of this pool using the system property java.util.concurrent.ForkJoinPool.common.parallelism.

Java 7中引入了fork/join框架,这是并行流在后台用于并行执行操作的基础结构.至关重要的是,必须对并行流内部有一个良好的了解,以便正确使用它们. fork/join框架旨在将可并行化的任务递归地拆分为较小的任务,然后组合每个子任务的结果以产生整体结果.这是ExecutorService界面的实现,该界面分发了 子任务分配给名为ForkJoinPool的线程池中的工作线程.

The infrastructure used behind the scenes by parallel streams to execute operations in parallel is the fork/join framework introduced in Java 7. It’s vital to have a good understanding of the parallel stream internals in order to use them correctly. The fork/join framework was designed to recursively split a parallelizable task into smaller tasks and then combine the results of each subtask to produce the overall result. It’s an implementation of the ExecutorService interface, which distributes those subtasks to worker threads in a thread pool, called ForkJoinPool.

Spliterator代表可拆分迭代器".与迭代器类似,拆分器用于遍历源元素,但它们也可以并行执行.尽管您可能不必在实践中开发自己的Spliterator,但了解如何进行开发将使您对并行流的工作方式有更广泛的了解.

The Spliterator stands for "splitable iterator." Like Iterators, Spliterators are used to traverse the elements of a source, but they’re also designed to do this in parallel. Although you may not have to develop your own Spliterator in practice, understanding how to do so will give you a wider understanding about how parallel streams work.

将Stream分成多个部分的算法是一个递归过程.第一步,在第一个分割器上调用称为trySplit的方法,并生成第二个方法.然后,在第2步中,再次调用这两个分离器,总共四个.框架一直在Spliterator上调用trySplit方法,直到返回null表示数据已通过 正在处理的结构不再可分割.最后,当所有Spliterator都向trySplit调用返回null时,此递归拆分过程终止.

The algorithm that splits a Stream into multiple parts is a recursive process. In the first step, a method called trySplit is invoked on the first Spliterator and generates a second one. Then in step 2 it’s called again on these two Spliterators, which results in a total of four. The framework keeps invoking the method trySplit on a Spliterator until it returns null to signal that the data structure that it’s processing is no longer divisible. Finally, this recursive splitting process terminates when all Spliterators have returned null to a trySplit invocation.

Spliterator接口声明的最后一个抽象方法是characteristics,该方法返回一个int,该int编码Spli​​terator本身的特征集. Spliterator客户端可以使用这些特征来更好地控制和优化其用法.它们是:ORDEREDDISTINCTSORTEDSIZEDNONNULLIMMUTABLECONCURRENTSUBSIZED.实际上,根据流的特定特性,它可能根本不并行运行.

The last abstract method declared by the Spliterator interface is characteristics, which returns an int encoding the set of characteristics of the Spliterator itself. The Spliterator clients can use these characteristics to better control and optimize its usage. They are: ORDERED, DISTINCT, SORTED, SIZED, NONNULL, IMMUTABLE, CONCURRENT, and SUBSIZED. Depending on the specific characteristics of a stream, it may in fact not run in parallel at all.

详细解释所有这些的书是: Java实际应用中的8:Lambda,流和函数式编程 (Raoul-Gabriel Urma,Mario Fusco和Alan Mycroft),来自曼宁.参见第7章: 并行数据处理和性能.

The book that explains all this in detail is: Java 8 in Action: Lambdas, streams, and functional-style programming (Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft), from Manning. See Chapter 7: Parallel data processing and performance.

这篇关于Java8的Collection.parallelStream如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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