Map源码解析之HashMap
Map源码解析之HashMap红黑树
Map源码解析之HashMap补充:集合、迭代器、compute、merge、replace
Map源码解析之LinkedHashMap
Map源码解析之TreeMap
Map源码解析之HashTable
Map源码解析之ConcurrentHashMap(JDK1.8)(一)
Map源码解析之ConcurrentHashMap(JDK1.8)(二)
Map源码解析之ConcurrentHashMap(JDK1.7)
Map源码解析之ThreadLocalMap
一、简述
WeakHashMap和ThreadLocalMap有些类似,用于存储key值为弱引用的键值对,会自动移除key值已经被回收的节点。
WeakHashMap的key和value都可以为null,但是key值会转化成空对象new Object()进行处理,防止其与过期节点出现混淆。
与ThreadLocalMap容易引发内存泄漏不同,WeakHashMap一般不会出现内存泄漏。但是在一些特殊场景下,WeakHashMap还是有可能出现内存泄漏,如https://blog.csdn.net/sanshiqiduer/article/details/3342222。
二、属性
/**
* The default initial capacity -- MUST be a power of two.
*/
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.
*/
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.
*/
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
*/
private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
/**
* The number of times this WeakHashMap has been structurally modified.
* Structural modifications are those that change the number of
* mappings in the map or otherwise modify its internal structure
* (e.g., rehash). This field is used to make iterators on
* Collection-views of the map fail-fast.
*
* @see ConcurrentModificationException
*/
int modCount;
除了queue 以外,其它属性都是Map类型中的常见属性。
关于ReferenceQueue的介绍,可以查看引用解析之Reference和ReferenceQueue。
三、Entry
V value;
final 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;
}
Entry类除了实现Map.Entry接口外,还继承了WeakReference类,key值为根据对象和引用队列生成的弱引用。
四、WeakHashMap#expungeStaleEntries()
private void expungeStaleEntries() {
for (Object x; (x = queue.poll()) != null; ) {
synchronized (queue) {
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>) x;
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
size--;
break;
}
prev = p;
p = next;
}
}
}
}
WeakHashMap中基于expungeStaleEntries方法对过期节点进行擦除。可以说,在WeakHashMap的所有操作、查询、遍历方法中都会调用该方法擦除过期节点。
在引用的状态为pengding时,引用将会被加入到对应的引用队列中,并且状态变为Enqueued。
因此queue属性中的元素对应的就是过期节点的key值,因此,该方法的逻辑就是遍历queue,将对应的节点从map中移除。
在擦除过程中,除了要将节点从链表中移除,还需要将其value值置为null,帮助GC对value对象进行回收,防止内存泄漏。
为了防止多线程操作引发的异常,查找并移除的逻辑需要放在synchronized锁中进行。
五、WeakHashMap#get(Object)
public V get(Object key) {
//null值处理
Object k = maskNull(key);
int h = hash(k);
//getTable中会调用expungeStaleEntries
Entry<K,V>[] tab = getTable();
int index = indexFor(h, tab.length);
Entry<K,V> e = tab[index];
while (e != null) {
if (e.hash == h && eq(k, e.get()))
return e.value;
e = e.next;
}
return null;
}
private Entry<K,V>[] getTable() {
expungeStaleEntries();
return table;
}
除了多了一个擦除机制外,get方法就是简单的数组+链表的查询。
六、WeakHashMap#remove(Object)
public V remove(Object key) {
Object k = maskNull(key);
int h = hash(k);
//getTable中会调用expungeStaleEntries
Entry<K,V>[] tab = getTable();
int i = indexFor(h, tab.length);
Entry<K,V> prev = tab[i];
Entry<K,V> e = prev;
while (e != null) {
Entry<K,V> next = e.next;
if (h == e.hash && eq(k, e.get())) {
modCount++;
size--;
if (prev == e)
tab[i] = next;
else
prev.next = next;
return e.value;
}
prev = e;
e = next;
}
return null;
}
除了多了一个擦除机制外,get方法就是数组定位+链表遍历的方式进行匹配并移除。
七、WeakHashMap#put(K, V)
public V put(K key, V value) {
Object k = maskNull(key);
int h = hash(k);
//getTable中会调用expungeStaleEntries
Entry<K,V>[] tab = getTable();
int i = indexFor(h, tab.length);
for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
if (h == e.hash && eq(k, e.get())) {
V oldValue = e.value;
if (value != oldValue)
e.value = value;
return oldValue;
}
}
modCount++;
Entry<K,V> e = tab[i];
tab[i] = new Entry<>(k, value, queue, h, e);
if (++size >= threshold)
resize(tab.length * 2);
return null;
}
根据key值进行数组定位+链表遍历,找到节点则value值覆盖,否则新建节点插入链表头部。
而后检查节点数量,大于扩容阈值threshold则进行resize处理
八、WeakHashMap#resize(int)
void resize(int newCapacity) {
Entry<K,V>[] oldTable = getTable();
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
Entry<K,V>[] newTable = newTable(newCapacity);
transfer(oldTable, newTable);
table = newTable;
/*
* If ignoring null elements and processing ref queue caused massive
* shrinkage, then restore old table. This should be rare, but avoids
* unbounded expansion of garbage-filled tables.
*/
if (size >= threshold / 2) {
threshold = (int)(newCapacity * loadFactor);
} else {
expungeStaleEntries();
transfer(newTable, oldTable);
table = oldTable;
}
}
resize是WeakHashMap的扩容方法,将数组长度扩充为原来的2倍,然后对节点进行数组元素复制,并将新数组作为map的数组。
因此在复制方法transfer中会忽略过期节点,所以扩容前后节点数量会发生变化。因此,复制后再次检查节点数量,如果数量不小于原阈值的1/2,则扩容成功,设置新阈值。否则调用擦除方法,并且将数组元素重新复制会旧数组,并重新使用旧数组作为map的数组。
private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) {
for (int j = 0; j < src.length; ++j) {
Entry<K,V> e = src[j];
src[j] = null;
while (e != null) {
Entry<K,V> next = e.next;
Object key = e.get();
if (key == null) {
e.next = null; // Help GC
e.value = null; // " "
size--;
} else {
int i = indexFor(e.hash, dest.length);
e.next = dest[i];
dest[i] = e;
}
e = next;
}
}
}
九、WeakHashMap#size()
public int size() {
if (size == 0)
return 0;
expungeStaleEntries();
return size;
}
可以看到,在返回属性size前,会先调用擦除方法。