并发 HashMap 线程安全和 happens-before 关系

Concurrent HashMap thread safety and happens-before relationship

ConcurrentHashMap 获取 javadoc,我阅读了以下关于上述集合的线程安全性的声明。

发件人:Class ConcurrentHashMap

Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update operations holding upon their onset. For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries.

我觉得这一段自相矛盾。准确地说,陈述 2 说检索反映了最近完成的操作,而陈述 3 几乎说聚合函数不能保证这种行为。

这是否意味着像 putAllclear 这样的聚合操作仍然是一个冒险的赌注?

Does this mean aggregate operations like putAll and clear are still a risky bet ?

他们 "retrieval operations...do not block" 的承诺对他们可以承诺的其他内容设置了一些主要限制。例如,map.get(k) 调用必须 立即 return null 或更早 put(k,v) 的某个 v相同 kget(k) 调用无法等待其他线程完成 map.putAll(someEnormousOtherMap) 调用。他们承诺不会阻塞!

基本上他们不能信守诺言,除非看起来是原子的唯一操作是 key/value 对的 insertions/removals/replacements。在不破坏非阻塞 get() 承诺的情况下实现聚合操作的唯一方法是将它们实现为对一次对一对 key/value 操作的原子原语的非原子调用序列。