boolean replace(K key, int hash, V oldValue, V newValue) { lock(); try { removeStale(); HashEntry<K,V> e = getFirst(hash); while (e != null && (e.hash != hash || !keyEq(key, e.key()))) e = e.next; boolean replaced = false; if (e != null && oldValue.equals(e.value())) { replaced = true; e.setValue(newValue, valueType, refQueue); } return replaced; } finally { unlock(); } }
try { if (!refRemove) removeStale(); int c = count - 1; HashEntry<K,V>[] tab = table;
V put(K key, int hash, V value, boolean onlyIfAbsent) { lock(); try { removeStale(); int c = count; if (c++ > threshold) {// ensure capacity
V replace(K key, int hash, V newValue) { lock(); try { removeStale(); HashEntry<K,V> e = getFirst(hash); while (e != null && (e.hash != hash || !keyEq(key, e.key()))) e = e.next; V oldValue = null; if (e != null) { oldValue = e.value(); e.setValue(newValue, valueType, refQueue); } return oldValue; } finally { unlock(); } }
/** * Removes any stale entries whose keys have been finalized. Use of this * method is normally not necessary since stale entries are automatically * removed lazily, when blocking operations are required. However, there * are some cases where this operation should be performed eagerly, such * as cleaning up old references to a ClassLoader in a multi-classloader * environment. * * Note: this method will acquire locks, one at a time, across all segments * of this table, so if it is to be used, it should be used sparingly. */ public void purgeStaleEntries() { for (int i = 0; i < segments.length; ++i) segments[i].removeStale(); }
/** * Reads value field of an entry under lock. Called if value * field ever appears to be null. This is possible only if a * compiler happens to reorder a HashEntry initialization with * its table assignment, which is legal under memory model * but is not known to ever occur. */ V readValueUnderLock(HashEntry<K,V> e) { lock(); try { removeStale(); return e.value(); } finally { unlock(); } }