'parallelStream' 函数的性能是否因原子变量而降低?

问题描述

假设我们有以下设置:

List<String> strings = Arrays.asList( "lore ipsum ..",... 10000 strings);
AtomicInteger count = new AtomicInteger(0);
strings.parallelStream().forEach( myString -> {
   System.out.println(count.incrementAndGet());
   System.out.println(myString); 
   // ... do more computing
});

如果我不使用原子变量,这段代码的执行速度会快得多吗? 使用原子变量是一个好习惯还是尽可能避免它们?

其他链接Parallel stream - race conditions with atomic variablesParallel processing

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)