2016-11-23 70 views
0
public boolean containsValue(@Nullable Object value) { 
    if (value == null) { 
     return false; 
    } 
    long now = ticker.read(); 
    final Segment<K, V>[] segments = this.segments; 
    long last = -1L; 
    for (int i = 0; i < CONTAINS_VALUE_RETRIES; i++) { 
     long sum = 0L; 
     for (Segment<K, V> segment : segments) { 
      // ensure visibility of most recent completed write 
      int unused = segment.count; // read-volatile 

      AtomicReferenceArray<ReferenceEntry<K, V>> table = segment.table; 
      for (int j = 0; j < table.length(); j++) { 
       for (ReferenceEntry<K, V> e = table.get(j); e != null; e = e.getNext()) { 
        V v = segment.getLiveValue(e, now); 
        if (v != null && valueEquivalence.equivalent(value, v)) { 
         return true; 
        } 
       } 
      } 
      sum += segment.modCount; 
     } 
     if (sum == last) { 
      break; 
     } 
     last = sum; 
    } 
    return false; 
} 

存在的containsValue mathed.My問題是,爲什麼只檢查modCount的沒有找到當值。如果其他線程調用之前「返回true」刪除操作,這mathed將wrong.forgive我英語不好。番石榴緩存中的containsValue

回答

1

是的,這可以競賽。那麼可以使用containsValue的結果進行任何操作;但是如果它返回true,那麼當方法正在被評估時,該值出現在某個點上;無論如何,這是你可能做得最好的。

modCount而言,只有當值不存在時纔會使用該值,因爲它用於檢測地圖是否正在同時修改並且需要再次查看。但是如果這個值在迭代過程中的任何時候出現,那麼這個方法應該返回true,它會這樣做。