■ 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; } } } }