平行流API中的波动性
请参阅下面的JDK流API收集器之一:
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
return new CollectorImpl<>(
() -> new int[1],
(a, t) -> { a[0] += mapper.applyAsInt(t); },
(a, b) -> { a[0] += b[0]; return a; },
a -> a[0], CH_NOID);
}
考虑使用并行处理,很明显,在任何同步中都不需要,并且可以安全地使用非电流容器() - &gt;新的int [1]
,由于每个这样的容器都是创建并填充累加器(a,t) - &gt; {a [a [0] += mapper.applyasint(t); }
在单独的线程中。然后将所有这些容器组合在单独的线程中,并使用组合仪(a,b) - &gt组合; {a [0] += b [0];返回a; }
进入最终结果。
同时,JLS假定如果该字段以某种方式在另一个线程中更新(由于线程缓存和其他JMM功能),则读取器线程可能不会看到非易失性字段的实际值。但是考虑到示例收集器,您可以看到使用了一个普通的int阵列,其元素并不动摇。我无法弄清楚为什么在这里使用普通的int数组,而不是例如atomicintegerarray,当非挥发元素int [0]
在一个线程中更新,然后在其他线程中读取执行组合。
您能解释一下为什么使用并行处理在流中使用具有非易失性力学的容器是安全的吗?
See one of the JDK stream api collectors below:
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
return new CollectorImpl<>(
() -> new int[1],
(a, t) -> { a[0] += mapper.applyAsInt(t); },
(a, b) -> { a[0] += b[0]; return a; },
a -> a[0], CH_NOID);
}
Considering using parallel processing, it is clear there is no need in any synchronization and it is safe to use a non-concurrent containers () -> new int[1]
, since each such container is created and filled with accumulator (a, t) -> { a[0] += mapper.applyAsInt(t); }
in a separate thread. Then all these containers are combined in a separate thread with combiner (a, b) -> { a[0] += b[0]; return a; }
into a final result.
Meanwhile, JLS postulates that the reader thread may not see the actual value of non-volatile field if this field was somehow updated in another thread (because of thread caching and other JMM features). But considering the example collector, you can see that a plain int array is used, the elements of which are not volatile. I can't figure out why a plain int array is used here, instead of e.g. AtomicIntegerArray, when the non-volatile element int[0]
is updated in one thread and then is read in other thread that performs combining.
Could you please explain why it is safe to use containers with non-volatile mechanics in stream api using parallel processing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它的工作方式是,每个并行线程都会收到其自己的个人副本(一个元素int数组),它们相互独立更新。阵列访问无需挥发性。
一旦线程用完源值,他们就会使用一个记忆障碍,以确保其单个数组进入正确发布,然后彼此“跳舞”以执行这些单个容器的组合。然后,每个这样的组合都具有另一个记忆障碍。这一直持续到只有一个容器。
实际实施涉及更多。查看 ReferencePipeline.Collect 。
The way it works is that every parallel thread receives it's own individual copies of the container (one element int array), which they update independently of each other. No volatile required for the array access.
Once the threads run out of source values, they use a memory barrier that ensures their individual array entry is properly published, then "dance" with each other to perform the combining of these individual containers. Each such combination is then followed with yet another memory barrier. This goes on until only one container remains.
The actual implementation is a bit more involved. Check out ReferencePipeline.collect.