ConcurrentHashMap原理解析

121 阅读1分钟

JDK1.8中ConcurrentHashMap源码分析

ConcurrentHashMap类中属性
transient volatile Node<K,V>[] table; CHM用来存储元素的数组


if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode());
int binCount = 0;
for (Node<K,V>[] tab = table;;) {
    Node<K,V> f; int n, i, fh;
    if (tab == null || (n = tab.length) == 0)
        tab = initTable();
    else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
        if (casTabAt(tab, i, null,
                     new Node<K,V>(hash, key, value, null)))
            break;                   // no lock when adding to empty bin
    }