在检查ConcurrentHashMap中是否存在密钥时,是否需要客户端锁定?

cel*_*tas 3 java concurrency multithreading concurrenthashmap

我知道我可以使用ConcurrentHashMap的putIfAbsent.但是,我需要进行webservice调用以获取给定键的值(如果它不存在)然后存储它(缓存类型),所以我不需要在下次使用相同的键时.以下哪项是正确的?我认为同步需要第二个版本.

更新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.
Run Code Online (Sandbox Code Playgroud)

Cos*_*atu 7

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

return KEY_VALUE_MAP.computeIfAbsent(key, this::doRestCall);
Run Code Online (Sandbox Code Playgroud)

编辑(以解决您的"无功能接口"约束):

如果要确保只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;
Run Code Online (Sandbox Code Playgroud)

但是,如果你想要强制执行对任何给定的密钥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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

作为(最后)附注,在您的代码示例中,您反转了使用put()putIfAbsent()(后者是在没有外部同步的情况下使用的那个),并且您为空检查读取了两次值.

  • @celeritas:是的,这就是我的意思,但是`key.intern()`有问题/不合适.最好构建自己的池,即将键映射到自身的冗余映射,以确保始终对同一个键使用相同的`String`实例.但是我认为你要走很长的路要避免额外的休息呼叫(这种情况只能在给定密钥存储任何映射之前发生,即使这样,概率也很低,imho).如果你没有注意到,一旦返回的值是非"空",就永远不会达到同步块. (2认同)