public V get(Object key) {
Segment<K,V> s; // manually integrate access methods to reduce overhead
HashEntry<K,V>[] tab;
int h = hash(key);
long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
(tab = s.table) != null) {
for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
(tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
e != null; e = e.next) {
K k;
if ((k = e.key) == key || (e.hash == h && key.equals(k)))
return e.value;
}
}
return null;
}
為什麼Vector的get就需要使用synchronized來加鎖而ConcurrentHashMap則不需要?而且CopyOnWriteArrayList也是使用了寫時複製還能實現讀寫並行,而顯然ConcrrentHashMap並沒有實現寫時複製,它是怎麼保證讀寫並行時不會讀取到不一致中間狀態的呢?