/** * The per-segment table. Elements are accessed via * entryAt/setEntryAt providing volatile semantics. */ transientvolatile HashEntry<K,V>[] table;
/** * The number of elements. Accessed only either within locks * or among other volatile reads that maintain visibility. */ transientint count;
/** * The total number of mutative operations in this segment. * Even though this may overflows 32 bits, it provides * sufficient accuracy for stability checks in CHM isEmpty() * and size() methods. Accessed only either within locks or * among other volatile reads that maintain visibility. */ transientint modCount;
public V get(Object key) { Segment<K,V> s; // manually integrate access methods to reduce overhead HashEntry<K,V>[] tab; inth= hash(key); longu= (((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; } } returnnull; }
在使用 get 操作时,会首先定位到某个 Segment,再定位该Segment上某个Index的元素,如果是条冲突链,就一直遍历完该链,而不管该链“已被遍历过的地方”发生了什么变化。注意这里的“已被遍历过的地方”,因为读的过程中可能某些元素会被其他线程改变,比如删除等,但由于其删除操作的特殊性,使得其支持并发性。