■ Java 引用的相關知識html
1. 強引用java
Object o = new Object();
2. 軟引用數組
public class SoftReference<T> extends Reference<T> {...}
3. 弱引用緩存
public class WeakReference<T> extends Reference<T> {...}
■ WeakHashMap 的認識:安全
1. 類定義數據結構
public class WeakHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>
2. 重要的全局變量多線程
/** * The default initial capacity -- MUST be a power of two. * 默認容量,必須是2次冪 */ private static final int DEFAULT_INITIAL_CAPACITY = 16; /** * The maximum capacity, used if a higher value is implicitly specified by either of the * constructors with arguments. MUST be a power of two <= 1<<30. * 最大容量,必須爲2次冪且 <= 1<<30 */ private static final int MAXIMUM_CAPACITY = 1 << 30; /** * The load factor used when none specified in constructor. * 負載因子 */ private static final float DEFAULT_LOAD_FACTOR = 0.75f; /** * The table, resized as necessary. Length MUST Always be a power of two. * 容量必須爲2次冪的數組 */ Entry<K,V>[] table; /** * The number of key-value mappings contained in this weak hash map. * 擁有鍵值對的數量 */ private int size; /** * The next size value at which to resize (capacity * load factor). * 閾值 -- 擴容判斷依據 */ private int threshold; /** * The load factor for the hash table. */ private final float loadFactor; /** * Reference queue for cleared WeakEntries * 引用隊列,用於存儲已被GC清除的WeakEntries */ private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
3. 構造器app
// 默認構造函數 WeakHashMap() // 指定"容量大小"的構造函數 WeakHashMap(int capacity) // 指定"容量大小"和"負載因子"的構造函數 WeakHashMap(int capacity, float loadFactor) // 包含"子Map"的構造函數 WeakHashMap(Map<? extends K, ? extends V> map)
4. Entry函數
/** * The entries in this hash table extend WeakReference, using its main ref field as the key. * 該Enty繼承WeakReference,從而具有弱引用的特性 */ private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> { V value; int hash; Entry<K,V> next;//鏈表 /** * Creates new entry. */ Entry(Object key, V value, ReferenceQueue<Object> queue, int hash, Entry<K,V> next) { super(key, queue); this.value = value; this.hash = hash; this.next = next; } .... }
■ WeakHashMap 的重要方法this
/** * Expunges stale entries from the table. -- 刪除過期的entry * 該方法是實現弱鍵回收的最關鍵方法,也是區分HashMap的根本方法 * 核心:移除table和queue的並集元素(queue中存儲是已被GC的key,注意是key!!) * 效果:key在GC的時候被清除,value在key清除後訪問WeakHashMap被清除 */ private void expungeStaleEntries() { //從隊列中出隊遍歷 //poll 移除並返問隊列頭部的元素;若是隊列爲空,則返回null for (Object x; (x = queue.poll()) != null; ) { synchronized (queue) { //值得一提的是WeakHashMap是非線程安全的,這裏卻同步了一下 //大神本來的用意是保證在多線程時能不破壞數據結構,但JavaDoc已經強調這類非安全,以下文 //http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6425537 @SuppressWarnings("unchecked") Entry<K,V> e = (Entry<K,V>) x; //找到該隊列中的元素在數組中的位置,並移除該元素(table和queue都須要移除) int i = indexFor(e.hash, table.length); Entry<K,V> prev = table[i]; Entry<K,V> p = prev; while (p != null) { Entry<K,V> next = p.next; if (p == e) { if (prev == e) table[i] = next; else prev.next = next; // Must not null out e.next; // stale entries may be in use by a HashIterator e.value = null; // Help GC 移除value size--; break; } prev = p; p = next; } } } }