java 8 流和并行流的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31769187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between java 8 streams and parallel streams
提问by Yogi Joshi
I wrote code using java 8 streams and parallel streams for the same functionality with a custom collector to perform an aggregation function.
When I see CPU usage using htop
, it shows all CPU cores being used for both `streams' and 'parallel streams' version. So, it seems when list.stream()is used, it also uses all CPUs. Here, what is the precise difference between parallelStream()and stream()in terms of usage of multi-core.
我使用 java 8 流和并行流编写代码以实现相同的功能,并使用自定义收集器来执行聚合功能。当我使用 看到 CPU 使用率时htop
,它显示所有 CPU 内核都用于“流”和“并行流”版本。因此,似乎当使用list.stream()时,它也使用所有 CPU。在这里,就multi-core的使用而言,parallelStream()和stream()之间的确切区别是什么。
回答by Hoopje
Consider the following program:
考虑以下程序:
import java.util.ArrayList;
import java.util.List;
public class Foo {
public static void main(String... args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
list.add(i);
}
list.stream().forEach(System.out::println);
}
}
You will notice that this program will output the numbers from 0 to 999 sequentially, in the order in which they are in the list. If we change stream()
to parallelStream()
this is not the case anymore (at least on my computer): all number are written, but in a different order. So, apparently, parallelStream()
indeed uses multiple threads.
您会注意到该程序将按照它们在列表中的顺序依次输出 0 到 999 之间的数字。如果我们更改stream()
为parallelStream()
这种情况,则不再是这种情况(至少在我的计算机上):所有数字都被写入,但顺序不同。所以,显然,parallelStream()
确实使用了多线程。
The htop
is explained by the fact that even single-threaded applications are divided over mutliple cores by most modern operating systems (parts of the same thread may run on several cores, but of course not at the same time). So if you see that a process used more than one core, this does not mean necessarily that the program uses multiple threads.
这htop
是因为即使是单线程应用程序也被大多数现代操作系统划分为多个内核(同一线程的部分可能在多个内核上运行,但当然不是同时运行)。因此,如果您看到一个进程使用了多个内核,这并不一定意味着该程序使用了多个线程。
Also the performance may not improve when using multiple threads. The cost of synchronization may nihilite the gains of using multiple threads. For simple testing scenarios this is often the case. For example, in the above example, System.out
is synchronized. So, effectively, only number can be written at the same time, although multiple threads are used.
使用多线程时,性能也可能不会提高。同步的成本可能会抵消使用多线程的好处。对于简单的测试场景,情况通常如此。例如,在上面的例子中,System.out
是同步的。因此,尽管使用了多个线程,但实际上只能同时写入数字。