JDK容器与并发—Map—WeakHashMap

WeakHashMap是一种基于弱引用键实现的非线程安全的HashMap。当key的引用被GC回收后,它会自动从Map中移除。value保持强引用,防止形成循环引用。在增删改查操作中,会检查并移除无效键值对。容量调整策略与HashMap类似,但在扩展时会特别处理弱引用的key。迭代器设计考虑了弱引用特性,使用了强引用辅助变量。
摘要由CSDN通过智能技术生成

概述

      基于弱引用键实现的HashMap,非线程安全。

1)当key所引用的对象只有弱引用时,在GC回收该对象后,会自动删除所关联的Entry;

2)其行为部分依赖GC;

3)value是强引用的。确保value没有直接或间接引用key,否则会阻止key引用对象的回收。间接引用:value1强引用key2的对象,其所关联的value2强引用key1的对象。

4)迭代器fail-fast。

数据结构

      与HashMap唯一的区别就是:WeakHashMap会将key封装到WeakReference,且关联ReferenceQueue,实现key的弱引用

Entry<K,V>[] table;

/**
* Reference queue for cleared WeakEntries
*/
private final ReferenceQueue<Object> queue = new ReferenceQueue<>();

private static class Entry<K,V> <strong>extends </strong><span style="color:#990000;">WeakReference<Object></span> 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) {
		<strong>super(key, queue);</strong>
		this.value = value;
		this.hash  = hash;
		this.next  = next;
	}
}

构造器

      与HashMap的区别是:table、threshold会在容器中完成初始化,没有提供init()扩展接口;HashMap会在第一个键值对put时完成table初始化:

// 带初始容量、负载因子构造
public WeakHashMap(int initialCapacity, float loadFactor) {
	if (initialCapacity < 0)
		throw new IllegalArgumentException("Illegal Initial Capacity: "+
										   initialCapacity);
	if (initialCapacity > MAXIMUM_CAPACITY)
		initialCapacity = MAXIMUM_CAPACITY;

	if (loadFactor <= 0 || Float.isNaN(loadFactor))
		throw new IllegalArgumentException("Illegal Load factor: "+
										   loadFactor);
	// table、loadFactor初始化
	int capacity = 1;
	while (capacity < initialCapacity)
		capacity <<= 1;
	table = newTable(capacity);
	this.loadFactor = loadFactor;
	threshold = (int)(capacity * loadFactor);
	useAltHashing = sun.misc.VM.isBooted() &&
			(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
}

// 带初始容量构造
public WeakHashMap(int initialCapacity) {
	this(initialCapacity, DEFAULT_LOAD_FACTOR);
}

// 无参构造
public WeakHashMap() {
	this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}

// 带Map构造
public WeakHashMap(Map<? extends K, ? extends V> m) {
	this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
			DEFAULT_INITIAL_CAPACITY),
		 DEFAULT_LOAD_FACTOR);
	putAll(m);
}

增删改查

清除WeakHashMap中无效键值对

      在增删改查的操作中,首先做的第一件事情就是调用getTable,从WeakHashMap中删除key引用的对象已回收的关联键值对:

/**
 * Returns the table after first expunging stale entries.
 */
private Entry<K,V>[] getTable() {
	expungeStaleEntries();
	return table;
}

// 从WeakHashMap中删除key引用的对象已回收的关联键值对
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
                    // 由于expungeStaleEntries的调用不会引起modCount的变化,
					// HashIterator的遍历感知不到其操作,所以next不能null
					e.value = null; // Help GC
					size--;
					break;
				}
				prev = p;
				p = next;
			}
		}
	}
}

容量调整策略

      与HashMap基本一样,只是在扩展过程中,需要考虑WeakHashMap的特性:

1)转移前首先从WeakHashMap中删除key引用的对象已回收的关联键值对,转移过程中已经回收的也不转移;

2)转移完成后,对于size低于threshold / 2的情况进行处理:容量不扩展,重新转移回来。

void resize(int newCapacity) {
	Entry<K,V>[] oldTable = getTable();// 从WeakHashMap中删除key引用的对象已回收的关联键值对:
	int oldCapacity = oldTable.length;
	if (oldCapacity == MAXIMUM_CAPACITY) {
		threshold = Integer.MAX_VALUE;
		return;
	}

	Entry<K,V>[] newTable = newTable(newCapacity);
	boolean oldAltHashing = useAltHashing;
	useAltHashing |= sun.misc.VM.isBooted() &&
			(newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
	boolean rehash = oldAltHashing ^ useAltHashing;
	transfer(oldTable, newTable, rehash);
	table = newTable;

	// 如果因为getTable、transfer去掉的关联键值对比较多,
	// size低于threshold / 2,则采用oldTable,将newTable中的键值对重新转移过来
	// 这种情况比较少,但对于WeakHashMap来说很有用,可以避免没有必要的扩展
	if (size >= threshold / 2) {
		threshold = (int)(newCapacity * loadFactor);
	} else {
		expungeStaleEntries(); // 转移前再次清除
		transfer(newTable, oldTable, false);
		table = oldTable;
	}
}

private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest, boolean rehash) {
	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) { // 对于“key所引用的对象已回收”的关联键值对不转移
				e.next = null;  // Help GC
				e.value = null; //  "   "
				size--;
			} else {
				if (rehash) {
					e.hash = hash(key);
				}
				int i = indexFor(e.hash, dest.length);
				e.next = dest[i];
				dest[i] = e;
			}
			e = next;
		}
	}
}

      增删改查的操作过程与HashMap一样。

迭代器

      与HashMap基本一样,除了因为 若引用特性而采用的两个强引用:nextKey、currentKey,分别用于next()能获取到、正常使用时可以获取到:

    private abstract class HashIterator<T> implements Iterator<T> {
        private int index;
        private Entry<K,V> entry = null;
        private Entry<K,V> lastReturned = null;
        private int expectedModCount = modCount;

        /**
         * Strong reference needed to avoid disappearance of key
         * between hasNext and next
         */
        private Object nextKey = null;

        /**
         * Strong reference needed to avoid disappearance of key
         * between nextEntry() and any use of the entry
         */
        private Object currentKey = null;

        HashIterator() {
            index = isEmpty() ? 0 : table.length;
        }

        public boolean hasNext() {
            Entry<K,V>[] t = table;

            while (nextKey == null) {
                Entry<K,V> e = entry;
                int i = index;
                while (e == null && i > 0)
                    e = t[--i];
                entry = e;
                index = i;
                if (e == null) {
                    currentKey = null;
                    return false;
                }
                nextKey = e.get(); // hold on to key in strong ref
                if (nextKey == null)
                    entry = entry.next;
            }
            return true;
        }

        /** The common parts of next() across different types of iterators */
        protected Entry<K,V> nextEntry() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            if (nextKey == null && !hasNext())
                throw new NoSuchElementException();

            lastReturned = entry;
            entry = entry.next;
            currentKey = nextKey;
            nextKey = null;
            return lastReturned;
        }

        public void remove() {
            if (lastReturned == null)
                throw new IllegalStateException();
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();

            WeakHashMap.this.remove(currentKey);
            expectedModCount = modCount;
            lastReturned = null;
            currentKey = null;
        }

    }

特性

      WeakHashMap的键值对的key为弱引用,其与HashMap的区别就是对key的这个特性的特殊处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值