java - 在检查 ConcurrentHashMap 中是否存在 key 时,是否需要客户端锁定?

标签 java multithreading concurrency concurrenthashmap

我知道我可以使用 ConcurrentHashMap 的 putIfAbsent。但是,如果给定键不存在,我需要调用 web 服务来获取它的值,然后存储它(一种缓存),这样下次使用相同的键时我就不需要这样做了。以下哪项是正确的?我认为第二个版本对于同步是必要的。

更新 1:我无法使用任何功能界面。

更新 2:根据 Costi Ciudatu 的回复更新代码片段

private static final Map<String, String> KEY_VALUE_MAP = new ConcurrentHashMap<>();
public String getValueVersion1(String key) {
    String value  = KEY_VALUE_MAP.get(key);
    if (value != null) {
        return value;
    }

    // Else fetch the value for the key from the webservice.
    value = doRestCall(key);
    KEY_VALUE_MAP.putIfAbsent(key, value);

    return value;
} // Version 1 Ends here.

public synchronized String getValueVersion2(String key) {
    String value  = KEY_VALUE_MAP.get(key);
    if (value != null) {
        return value;
    }

    // Else fetch the value for the key from the webservice.
    value = doRestCall(key);
    KEY_VALUE_MAP.put(key, value);
    return value;
} // Version 2 ends here.

最佳答案

你应该看看ConcurrentMap#computeIfAbsent它以原子方式为你做这件事:

return KEY_VALUE_MAP.computeIfAbsent(key, this::doRestCall);

编辑(解决您的“无功能界面”限制):

如果您想确保只为任何给定的键调用一次doRestCall,您只需要“客户端锁定”。否则,这段代码会工作得很好:

final String value = KEY_VALUE_MAP.get(key);
if (value == null) {
    // multiple threads may call this in parallel
    final String candidate = doRestCall(key);
    // but only the first result will end up in the map
    final String winner = KEY_VALUE_MAP.putIfAbsent(key, candidate);
    // local computation result gets lost if another thread made it there first
    // otherwise, our "candidate" is the "winner"
    return winner != null ? winner : candidate;
}
return value;

但是,如果您确实想强制doRestCall 对任何给定键只调用一次(我的猜测是您不会确实需要这个),您将需要某种同步。但在您的示例中尝试比“全有或全无”方法更有创意:

final String value = KEY_VALUE_MAP.get(key);
if (value != null) {
    return value;
}
synchronized(KEY_VALUE_MAP) {
    final String existing = KEY_VALUE_MAP.get(key);
    if (existing != null) {  // double-check
        return existing;
    }
    final String result = doRestCall(key);
    KEY_VALUE_MAP.put(key, result);  // no need for putIfAbsent
    return result;
}

如果您想使用第二种(偏执)方法,您还可以考虑使用 key 本身进行锁定(将范围缩小到最小)。但这可能需要您管理自己的 key 池,因为 syncrhonized (key.intern()) 不是好的做法。

这一切都依赖于您的 doRestCall() 方法永远不会返回 null 这一事实。否则,您必须将 map 值包装在 Optional(或一些自制的 pre-java8 替代方案)中。

作为(最后的)旁注,在您的代码示例中,您颠倒了 put()putIfAbsent() 的使用(后者是与无外部同步)并且您读取该值两次以进行空值检查。

关于java - 在检查 ConcurrentHashMap 中是否存在 key 时,是否需要客户端锁定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44742297/

相关文章:

java - 在 Eclipse RCP 应用程序中捕获终止信号

java - 根据条件动态提交任务到ExecutorService

java - sql 查询在 Hibernate 中很慢,在 mysql 上很快

java - 如何解析 freedict 文件(*.dict 和 *.index)

java - 从现有的 byte[] 生成新的 byte[]

java - synchronized java关键字是如何实现的?

Java线程向多个类发送相同的数据

java - 启动线程作为最终类的构造函数的最后一条语句

c# - 调整并发字典大小时遇到​​ Synchronization LockException

java - compare-and-swap和blocking算法的性能比较