简体   繁体   English

ConcurrentHashMap非阻塞读取和内存可见性问题

[英]ConcurrentHashMap non-blocking reads and memory visibility issue

ConcurrentHashMap in Java offers reads to proceed concurrently with updates. Java中的ConcurrentHashMap提供读取以与更新同时进行。 The trade-off in this is that the results of the read is limited to reflect only the last completed update when the reading began, so it is not specified to reflect the latest state of elements. 在这方面的权衡是读取的结果仅限于反映读数开始时的最后完成更新,因此未指定反映元素的最新状态。

However AFAIK Java Memory Model , without some form of synchronization between the read and write threads, the updates of the write thread may not become visible to the read thread , even after arbitrary period of time. 但是,AFAIK Java内存模型,在读写线程之间没有某种形式的同步,即使在任意一段时间之后,写线程的更新也可能对读线程不可见。

Given the read threads do not block with write threads , what forms the basis of the guarantee of visibility of the last completed update to be available to the read thread ? 鉴于读取线程不会阻塞写入线程,是什么形成了保证读取线程可用的上次完成更新的可见性的基础?

I could only think of something on the lines of Compare-and-swap algorithm at play but I could not verify it in the source code of that labrary. 我只能在比较和交换算法的方面考虑一些事情,但我无法在该工程的源代码中验证它。

The read of the values are actually volatile loads. 读取值实际上是易失性负载。 Though it is non-blocking you will ensure the happens-before relationship since the store too is volatile. 虽然它是非阻塞的,但您将确保之前发生的关系,因为商店也是易变的。

Java 5,6,7's version of the CHM does not use CAS to swap the references. Java 5,6,7版本的CHM不使用CAS来交换引用。 But there is a newer lightweight version in the works that would use in some of its writes. 但是在工作中有一个更新的轻量级版本可用于它的一些写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM