java - ConcurrentHashMap 的计算、computeIfAbsent 和computeIfPresent 方法是完全原子的吗?

标签 java concurrency thread-safety atomic concurrenthashmap

整个方法调用是原子的还是只是 BiFunction 执行的原子?是阻塞所有键还是只阻塞同一键上的调用?

最佳答案

以下详细信息适用于 OpenJDK Java 11。

这三种方法持有 Node 的锁在 map 中调用方法并更新键/值。该节点通常是哈希桶的节点链或节点树中的第一个节点。在同一存储桶中插入、更新或删除键/值对的并发尝试将被阻止,直到锁定被释放。

(其他版本的 Java 的行为可能有所不同。)

Is the whole method call atomic or is just the BiFunction execution atomic?



整个方法调用。

Is it blocking for all keys or just for the calls on the same key?



介于两者之间;看上面。但是,如果您遵循 javadocs 中的建议,应该没有关系。

" Some attempted update operations on this map by other threads may be blocked while computation is in progress, so the computation should be short and simple ..."

关于java - ConcurrentHashMap 的计算、computeIfAbsent 和computeIfPresent 方法是完全原子的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59600676/

相关文章:

java - HttpURLConnection 发布 Android

Java 线程 : Should all shared variables be Volatile ?

java - 哈希表中的并发性和故障安全行为

Python:os.read()/os.write() 在 os.pipe() 线程安全吗?

Java - 像普通代码一样运行字符串

java - 如何反序列化当前对象

java - tryLock 未释放的资源

arrays - 通用 Lisp : Why does lparallel have problems with assigning array elements?

java - 如何添加向右滑动打开抽屉导航

java - 以负载均衡方式在 4 个线程上分配 10 个无限作业 (Java)