Skip to content

Latest commit

 

History

History
79 lines (59 loc) · 1.67 KB

map-weakhashmap.md

File metadata and controls

79 lines (59 loc) · 1.67 KB

WeakHashMap<K,V>

设计

链地址法,不转红黑树。

使用 WeakReferenceEntry 利用 GC 来回收条目

扩容策略

/**
* The default initial capacity -- MUST be a power of two.
*/
private static final int DEFAULT_INITIAL_CAPACITY = 16;

/**
* The load factor used when none specified in constructor.
*/
private static final float DEFAULT_LOAD_FACTOR = 0.75f;

达到阈值直接扩容为原来两倍

if (++size >= threshold)
    resize(tab.length * 2);

开源组件拓展

Tomcat

public final class ConcurrentCache<K,V> {

    private final int size;

    private final Map<K,V> eden;

    private final Map<K,V> longterm;

    public ConcurrentCache(int size) {
        this.size = size;
        this.eden = new ConcurrentHashMap<>(size);
        this.longterm = new WeakHashMap<>(size);
    }

    public V get(K k) {
        V v = this.eden.get(k);
        if (v == null) {
            synchronized (longterm) {
                v = this.longterm.get(k);
            }
            if (v != null) {
                this.eden.put(k, v);
            }
        }
        return v;
    }

    public void put(K k, V v) {
        if (this.eden.size() >= size) {
            synchronized (longterm) {
                this.longterm.putAll(this.eden);
            }
            this.eden.clear();
        }
        this.eden.put(k, v);
    }
}

参考