- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Java 8,想知道 ConcurrentHashMap
的 computeIfPresent
操作是否锁定了整个表/映射或仅锁定包含 key 的 bin。
来自documentation computeIfPresent
方法:
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, and must not attempt to update any other mappings of this map
当为一个键调用这个方法时,看起来整个 map 都被锁定了。如果更新某个键的值,为什么必须锁定整个 map ?只锁定包含键/值对的容器不是更好吗?
最佳答案
从实现来看(Oracle JDK 1.8.0_101),只是相应的bin被锁定了。这与您引用的文档片段并不矛盾,因为它提到某些 更新操作可能会被阻止,不一定是全部。当然,如果文档明确说明锁定的内容会更清楚,但那会将实现细节泄露给接口(interface)的事实上部分。
关于java - Java 中的 ConcurrentHashMap 锁定机制用于 computeIfPresent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41916368/
computeIfPresent 是 java.util.Map 的默认方法,已在 Java 8 中引入。computeIfPresent 方法为给定键及其关联值计算指定的Map函数,然后更新该键的值
假设我有一个以集合作为值的并发映射: Map map = new ConcurrentHashMap<>(); map.putIfAbsent(8, new ArrayList<>()); 然后我按如
我正在尝试制作具有以下功能的工厂: 它应该总是返回一个 Filter 对象。 从 hashmap = 如果字符串字母(键)已经存在于 hashmap 中,它应该只从 hashmap 中获取它的值(对象
我有一组非常适合我的代码: for (String word : distinctWordsInOneLigne) { Map> map = new H
我有以下代码: for (String val: values) { EnumType type = EnumType.get(val); if (type != null) {
这是我原来的 SO question 的跟进问题. 感谢这个问题的答案,根据 ConcurrentMap.computeIfPresent javadoc 看起来是这样的 The default im
workObjectMap.computeIfPresent(key, (k,v) -> { v.memberIdSet.addAll(memberIdSet); v.memberPo
我正在使用 Java 8,想知道 ConcurrentHashMap 的 computeIfPresent 操作是否锁定了整个表/映射或仅锁定包含 key 的 bin。 来自documentation
我用过计算方法和合并方法。但是我仍然不确定合并方法与计算方法有何不同。 我在采访中被问到一个问题,以维护给定 IP 地址列表的命中计数器。这是 Map 的基本实现,其中键是 IP,值是命中数。 我用
我有这段代码: if (notificationSend.get(key) != null && notificationSend.get(key).equals(value)) { retur
jdk 8新版Concurrent Hash Map新增了两个Method。 computeIfAbsent computeIfPresent putIfAbsent - 旧方法 我了解 putIfA
当尝试使用 computeIfPresent() 方法更改 map 时,我在使用 innerMap 时遇到了实现此方法的问题。 这个有效: Map mapOne = new HashMap<>();
我正在尝试查看实际的 Java 文档,描述传递给 ConcurrentHashMap.computeIfAbsent 和 ConcurrentHashMap.computeIfPresent< 时可以
我是一名优秀的程序员,十分优秀!