Why parallel stream get collected sequentially in Java 8(为什么在 Java 8 中按顺序收集并行流)
问题描述
为什么 forEach
以随机顺序打印数字,而 collect
总是按原始顺序收集元素,即使从并行流中也是如此?
Why forEach
prints numbers in random order, while collect
always collects elements in original order, even from parallel stream?
Integer[] intArray = {1, 2, 3, 4, 5, 6, 7, 8};
List<Integer> listOfIntegers = new ArrayList<>(Arrays.asList(intArray));
System.out.println("Parallel Stream: ");
listOfIntegers
.stream()
.parallel()
.forEach(e -> System.out.print(e + " "));
System.out.println();
// Collectors
List<Integer> l = listOfIntegers
.stream()
.parallel()
.collect(Collectors.toList());
System.out.println(l);
输出:
Parallel Stream:
8 1 6 2 7 4 5 3
[1, 2, 3, 4, 5, 6, 7, 8]
推荐答案
这里有两种不同的排序",这让讨论变得混乱.
There are two different kinds of "ordering" going on here, which makes the discussion confusing.
一种是遭遇订单,定义在流文档.考虑这一点的一个好方法是源集合中元素的空间或从左到右顺序.如果源是 List
,则认为较早的元素位于后面的元素的左侧.
One kind is encounter order, which is defined in the streams documentation. A good way to think about this is the spatial or left-to-right order of elements in the source collection. If the source is a List
, consider the earlier elements being to the left of later elements.
还有 processing 或 temporal 顺序,文档中没有定义,但它是不同线程处理元素的时间顺序.如果列表的元素正在由不同的线程并行处理,则线程可能会在最左边的元素之前处理列表中最右边的元素.但下次可能不会.
There is also processing or temporal order, which isn't defined in the documentation, but which is the time order in which elements are processed by different threads. If the elements of a list are being processed in parallel by different threads, a thread might process the rightmost element in the list before the leftmost element. But the next time it might not.
即使计算是并行完成的,大多数收集器
和一些终端操作都经过精心安排,以便它们保持从源到目的地的遭遇顺序,独立于不同线程可能处理每个元素的时间顺序.
Even when computations are done in parallel, most Collectors
and some terminal operations are carefully arranged so that they preserve encounter order from the source through to the destination, independently of the temporal order in which different threads might process each element.
请注意,forEach
终端操作不 保留遇到顺序.相反,它由碰巧产生下一个结果的任何线程运行.如果您想要像 forEach
这样保留遇到顺序的东西,请改用 forEachOrdered
.
Note that the forEach
terminal operation does not preserve encounter order. Instead, it's run by whatever thread happens to produce the next result. If you want something like forEach
that preserves encounter order, use forEachOrdered
instead.
另请参阅 Lambda 常见问题解答 进一步讨论订购问题.
See also the Lambda FAQ for further discussion about ordering issues.
这篇关于为什么在 Java 8 中按顺序收集并行流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么在 Java 8 中按顺序收集并行流
基础教程推荐
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01