在ConcurrentHashMap.computeIfAbsent和ConcurrentHashMap.computeIfPresent中执行`mappingFunction` [英] Execution of `mappingFunction` in ConcurrentHashMap.computeIfAbsent and ConcurrentHashMap.computeIfPresent

查看:155
本文介绍了在ConcurrentHashMap.computeIfAbsent和ConcurrentHashMap.computeIfPresent中执行`mappingFunction`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图查看实际的Java文档,描述传递给 ConcurrentHashMap.computeIfAbsent mappingFunction 多少次的行为c $ c>和 ConcurrentHashMap.computeIfPresent 方法。

I am trying to see actual Java documentation describing the behavior of how many times mappingFunction can be called when passed to ConcurrentHashMap.computeIfAbsent and ConcurrentHashMap.computeIfPresent methods.

的Javadoc> ConcurrentHashMap.computeIfAbsent 似乎很清楚地说 mappingFunction 最多只会执行一次:

The Javadoc of ConcurrentHashMap.computeIfAbsent seems pretty clear in saying that the mappingFunction will be executed at most once:

的Javadoc> ConcurrentHashMap.computeIfAbsent

Javadoc for ConcurrentHashMap.computeIfAbsent


如果指定的键尚未与某个值相关联,请尝试
使用给定的映射函数计算其值并将
输入此地图,除非为null。整个方法调用以原子方式执行
因此每个键最多应用一次该函数。其他线程在此映射上尝试更新操作的某些
可能在计算正在进行时被阻止
,因此计算应该是
简短且简单,并且不得尝试更新任何其他映射
这张地图。

If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null. The entire method invocation is performed atomically, so the function is applied at most once per key. 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.

的Javadoc> ConcurrentHashMap.computeIfPresent 没有说明可以执行多少次 mappingFunction

But the Javadoc for ConcurrentHashMap.computeIfPresent does not say anything about how many times mappingFunction can be executed:

<$ c $的Javadoc c> ConcurrentHashMap.computeIfPresent

Javadoc for ConcurrentHashMap.computeIfPresent


如果指定键的值存在,则尝试在给定密钥及其当前映射值的情况下计算
新映射。整个
方法调用以原子方式执行。当
计算正在进行时,其他线程可能会阻止某些尝试在此映射上更新
操作,因此计算应该很短且
简单,并且不得尝试更新任何其他映射这张地图。

If the value for the specified key is present, attempts to compute a new mapping given the key and its current mapped value. The entire method invocation is performed atomically. 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.

通过查看源代码,它们看起来都像 mappingFunction 最多只执行一次。但我真的希望看到保证这种行为的实际文件。

By looking at the source code they both look like that mappingFunction will be executed at most once. But I would really like to see actual documentation guaranteeing that behavior.

是否有这样的文件?

推荐答案

ConcurrentMap#computeIfPresent ,我们看到以下内容:

In the documentation for ConcurrentMap#computeIfPresent, we see the following:


默认实现相当于对此地图执行以下步骤:

The default implementation is equivalent to performing the following steps for this map:



for (V oldValue; (oldValue = map.get(key)) != null; ) {
    V newValue = remappingFunction.apply(key, oldValue);
    if ((newValue == null)
        ? map.remove(key, oldValue)
        : map.replace(key, oldValue, newValue))
     return newValue;
 }
 return null;

尽管文档没有明确说明重映射功能只会执行一次,但相当于文档提供的代码清楚明了。

Even though the documentation doesn't explicitly say that the remapping function will only be executed once, the equivalent code that the documentation provides makes that clear.

注意:请记住:


当多个线程尝试更新时,地图操作和重新映射功能可能被多次调用

(强调我的)

这篇关于在ConcurrentHashMap.computeIfAbsent和ConcurrentHashMap.computeIfPresent中执行`mappingFunction`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆