gpt4 book ai didi

java - putIfAbsent 方法是否阻塞?

转载 作者:行者123 更新时间:2023-11-29 04:45:09 24 4
gpt4 key购买 nike

我想在我的应用程序中使用 ConcurrentSkipListMap,但不太确定如何处理它。查看源代码,我没有找到任何 Lock 获取的 synchronized 语句。

Documentation也不是很清楚。它只是说该操作以原子方式执行。

那么,public K putIfAbsent(K, V) 是非阻塞的吗?

最佳答案

关于 java.util.concurrent.Concurrent* API 的注释

不幸的是,java.util.concurrent.Concurrent* 类型的 Javadoc 并不像 ConcurrentHashMap 那样具体说明如何实现并发保证。 :

A hash table supporting full concurrency of retrievals and high expected concurrency for updates. This class obeys the same functional specification as java.util.Hashtable, and includes versions of methods corresponding to each method of Hashtable. However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access. This class is fully interoperable with Hashtable in programs that rely on its thread safety but not on its synchronization details.

从本质上讲,多年来它表明早期的 Hashtable在锁定方面非常激进的实现无法扩展到非常大且广泛使用和共享的 map 。对于区分读锁和写锁的数据库,需要更高效的东西。

你的具体方法

您的特定方法是“非阻塞”的,因为它不使用监视器来保证一致性和原子性。它依赖于 JVM 对 volatile 引用的内存模型保证,您在整个实现过程中都会遇到这种情况。这些实现中特别有趣的是您可以看到这样的循环(在私有(private) doPut() 方法中):

outer: for (;;) {
for (Node<K,V> b = findPredecessor(key, cmp), n = b.next;;) {
...
break; // restart if lost race to replace value

因此,竞争线程同时操作相同的对象而不会阻塞,但它们可能需要多次尝试才能成功。换句话说,CPU 不是阻塞,而是“燃烧”。

关于java - putIfAbsent 方法是否阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37496862/

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