gpt4 book ai didi

java - ConcurrentMap 中的内存一致性影响

转载 作者:搜寻专家 更新时间:2023-11-01 02:20:35 25 4
gpt4 key购买 nike

根据ConcurrentMap Javadoc :

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a ConcurrentMap as a key or value happen-before actions subsequent to the access or removal of that object from the ConcurrentMap in another thread.

上面的语句是什么意思?它是如何工作的,因为 ConcurrentHashMap 中的 get() 方法没有阻塞(例如与 BlockingQueue 相比)?

最佳答案

意思很简单。假设您有两段代码:

 a = new A();
b = ...
someConcurrentHashMap.put(b, whatever);

然后:

Whatever value = someConcurrentHashMap.get(b);
c = new C();

b同一个对象,并且这两段代码分别由两个不同的线程执行时,那么可以保证a = new A() 发生在 c = new C()之前。

有关“发生在之前”的进一步阅读 - 参见 here .

关于实现细节,我建议你研究源码code - 包含大量(非 javadoc!)解释此类内部工作原理的注释。

关于java - ConcurrentMap 中的内存一致性影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45342911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com