微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – stream和parallelStream

我有一个像这样的测试代码
List<Integer> list = new ArrayList<>(1000000);

for(int i=0;i<1000000;i++){
    list.add(i);
}

List<String> values = new ArrayList<>(1000000);

list.stream().forEach(
    i->values.add(new Date().toString())
);

System.out.println(values.size());

运行这个,我得到了一个正确的输出:1000000.

但是,如果我将stream()更改为parallelStream(),如下所示:

list.parallelStream().forEach(
    i->values.add(new Date().toString())
 );

我有一个随机输出,例如:920821.

怎么了?

解决方法

ArrayList未同步.未定义尝试同时向其添加元素.从 forEach开始:

For parallel stream pipelines,this operation does not guarantee to respect the encounter order of the stream,as doing so would sacrifice the benefit of parallelism. For any given element,the action may be performed at whatever time and in whatever thread the library chooses.

在第二个示例中,您最终会同时在阵列列表上调用add多个线程,并且ArrayList文档说:

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently,and at least one of the threads modifies the list structurally,it must be synchronized externally.

错误解决方

如果将ArrayList的使用更改为Vector,则会得到正确的结果,因为此列表实现是同步的.它的Javadoc说:

Unlike the new collection implementations,Vector is synchronized.

但是,do not use it!此外,由于显式同步,它可能最终会变慢.

正确的方法

显然是为了避免Stream API使用collect方法提供mutable reduction范例的这种情况.下列

List<String> values = list.stream().map(i -> "foo").collect(Collectors.toList());

无论是否并行运行,都将始终提供正确的结果. Stream管道在内部处理并发和guarantees that it is safe to use a non-concurrent collector in a collect operation of a parallel stream. Collectors.toList()一个内置的收集器,将Stream的元素累积到列表中.

原文地址:https://www.jb51.cc/java/127476.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐