Вопрос по concurrency, hashmap, java – ConcurrentHashMap put против putIfAbsent

17

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededputIfAbsentError: User Rate Limit Exceeded

<code>   if (!map.containsKey(key)) 
      return map.put(key, value);
   else
      return map.get(key);
</code>

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

<code>public void AddToCache(T key, V value)
{
   V local = _cache.putifabsent(key, value);

   if(local.equals(value) && local.IsExpired() == false){
     return;
   }
   // this is for updating the cache with a new value
   _cache.put(key, value);
}
</code>
Error: User Rate Limit Exceeded Gray
Error: User Rate Limit ExceededpreviousError: User Rate Limit Exceeded Marko Topolnik
Error: User Rate Limit Exceeded DarthVader
Error: User Rate Limit ExceededlocalError: User Rate Limit ExceededvalueError: User Rate Limit ExceedednotError: User Rate Limit ExceededvalueError: User Rate Limit Exceeded Brian Roach

Ваш Ответ

2   ответа
5

concurrentputIfAbsent()

Error: User Rate Limit Exceeded
11

Error: User Rate Limit Exceeded

would this be a better impl for adding and updating cache?

Error: User Rate Limit Exceeded

1.Error: User Rate Limit ExceededputIfAbsentError: User Rate Limit Exceededmap.getError: User Rate Limit Exceeded

    V local = _cache.get(key);
    if (local.equals(value) && !local.IsExpired()) {
        return;
    }

2.Error: User Rate Limit ExceededifError: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

public void AddToCache(T key, V value) {
    for (;;) {

        V local = _cache.get(key);
        if(local == null){
            local = _cache.putIfAbsent(key, value);
            if(local == null)
                return;
        }
        if (local.equals(value) && !local.IsExpired()) {
            return;
        }

        if (_cache.replace(key, local, value))
            return;
    }
}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы