ConcurrentHashMap是一个支持高并发集合,常用的集合之一,在jdk1.8中ConCurrentHashMap的结构和操作和hashMap都很类似:

唯一不同的是,HashMap不支持并发操作,ConCurrentHashMap是支持并发操作的。所以ConCurrentHashMap的设计也比HashMap也复杂的多,通过阅读ConCurrentHashMap的源码,也更加了解一些并发的操作,比如:

详见HashMap相关文章:

详解HashMap源码解析(上)

详解HashMap源码解析(下)

数据结构

ConCurrentHashMap是由数组 链表/红黑树组成的:

current hashmap1.7与1.8的区别(详解ConCurrentHashMap源码jdk1.8)(1)

其中左侧部分是一个哈希表,通过hash算法确定元素在数组的下标:

transient volatile Node<K,V>[] table;

链表是为了解决hash冲突,当发生冲突的时候。采用链表法,将元素添加到链表的尾部。其中Node节点存储数据:

static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; volatile V val; volatile Node<K,V> next; Node(int hash, K key, V val, Node<K,V> next) { this.hash = hash; this.key = key; this.val = val; this.next = next; } }

Node节点包含:

主要属性字段

// 最大容量 int MAXIMUM_CAPACITY = 1 << 30; // 初始化容量 int DEFAULT_CAPACITY = 16 // 控制数组初始化或者扩容,为负数时,表示数组正在初始化或者扩容。-1表示正在初始化。其他情况-n表示n线程正在扩容。 private transient volatile int sizeCtl; // 装载因子 float LOAD_FACTOR = 0.75f // 链表长度为 8 转成红黑树 int TREEIFY_THRESHOLD = 8 // 红黑树长度小于6退化成链表 int UNTREEIFY_THRESHOLD = 6;

获取数据get

public V get(Object key) { Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek; // 计算hash值 int h = spread(key.hashCode()); // 判断 tab 不为空并且 tab对应的下标不为空 if ((tab = table) != null && (n = tab.length) > 0 && (e = tabAt(tab, (n - 1) & h)) != null) { if ((eh = e.hash) == h) { if ((ek = e.key) == key || (ek != null && key.equals(ek))) return e.val; } // eh < 0 表示遇到扩容 else if (eh < 0) return (p = e.find(h, key)) != null ? p.val : null; // 遍历链表,直到遍历key相等的值 while ((e = e.next) != null) { if (e.hash == h && ((ek = e.key) == key || (ek != null && key.equals(ek)))) return e.val; } } return null; }

get如何实现线程安全

get方法里面没有使用到锁,那是如何实现线程安全。主要使用到了volatile。

一个线程对共享变量的修改,另外一个线程能够立刻看到,我们称为可见性。

CPU运行速度比内存速度快很多,为了均衡和内存之间的速度差异,增加了cpu缓存,如果在cpu缓存中存在cpu需要数据,说明命中了cpu缓存,就不经过访问内存。如果不存在,则要先把内存的数据载入到cpu缓存中,在返回给cpu处理器。

在多核cpu的服务器中,每个cpu都有自己的缓存,cpu之间的缓存是不共享的。 当多个线程在不同的cpu上执行时,比如下图中,线程A操作的是cpu-1上的缓存,线程B操作的是cpu-2上的缓存,这个时候,线程A对变量V的操作对于线程B是不可见的。

current hashmap1.7与1.8的区别(详解ConCurrentHashMap源码jdk1.8)(2)

但是一个变量被volatile声明,它的意思是:

告诉编译器,对这个变量的读写,不能使用cpu缓存,必须从内存中读取或者写入。

上面的变量V被volatile声明,线程A在cup-1中修改了数据,会直接写到内存中,不会写入到cpu缓存中。而线程B无法从cpu缓存读取变量,需要从主内存拉取数据。

volatile在get应用

transient volatile Node<K,V>[] table;

使用volatile声明数组,表示引用地址是volatile而不是数组元素是volatile。

既然不是数组元素被修饰成volatile,那实现线程安全在看Node节点。

static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; volatile V val; volatile Node<K,V> next; }

其中val和next都用了volatile修饰,在多线程环境下,线程A修改节点val或者新增节点对别人线程是可见的。 所以get方法使用无锁操作是可以保证线程安全。

既然volatile修饰数组对get操作没有效果,那加在volatile上有什么目的呢?

是为了数组在扩容的时候对其他线程具有可见性。

添加数据put

put(K key, V value)直接调用putVal(key, value, false)方法。

public V put(K key, V value) { return putVal(key, value, false); }

putVal()方法:

final V putVal(K key, V value, boolean onlyIfAbsent) { // key或者value为空,报空指针错误 if (key == null || value == null) throw new NullPointerException(); // 计算hash值 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为空或者长度为0,初始化table tab = initTable(); // 使用volatile查找索引下的数据 else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) { // 索引位置没有数据,使用cas添加数据 if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value, null))) break; // no lock when adding to empty bin } // MOVED表示数组正在进行数组扩容,当前进行也参加到数组复制 else if ((fh = f.hash) == MOVED) tab = helpTransfer(tab, f); else { V oldVal = null; // 数组不在扩容和也有值,说明数据下标处有值 // 链表中有数据,使用synchronized同步锁 synchronized (f) { if (tabAt(tab, i) == f) { // 为链表 if (fh >= 0) { binCount = 1; // 遍历链表 for (Node<K,V> e = f;; binCount) { K ek; // hash 以及key相同,替换value值 if (e.hash == hash && ((ek = e.key) == key || (ek != null && key.equals(ek)))) { oldVal = e.val; if (!onlyIfAbsent) e.val = value; break; } Node<K,V> pred = e; // 遍历到链表尾,添加链表节点 if ((e = e.next) == null) { pred.next = new Node<K,V>(hash, key, value, null); break; } } } // 红黑树,TreeBin哈希值固定为-2 else if (f instanceof TreeBin) { Node<K,V> p; binCount = 2; if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key, value)) != null) { oldVal = p.val; if (!onlyIfAbsent) p.val = value; } } } } if (binCount != 0) { // 链表转红黑树 if (binCount >= TREEIFY_THRESHOLD) treeifyBin(tab, i); if (oldVal != null) return oldVal; break; } } } addCount(1L, binCount); return null; }

ConcurrentHashMap键值对为什么都不能为null,而HashMap就可以?

通过get获取数据时,如果获取的数据是null,就无法判断,是put时的value为null,还是找个key就没做过映射。HashMap是非并发的,可以通过contains(key)判断,而支持并发的ConcurrentHashMap在调用contains方法和get方法的时候,map可能已经不同了。参考

如果数组table为空调用initTable初始化数组:

private final Node<K,V>[] initTable() { Node<K,V>[] tab; int sc; // table 为 null while ((tab = table) == null || tab.length == 0) { if ((sc = sizeCtl) < 0) // sizeCtl<0表示其它线程正在初始化数组数组,当前线程需要让出CPU Thread.yield(); // lost initialization race; just spin // 调用CAS初始化table表 else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) { try { if ((tab = table) == null || tab.length == 0) { int n = (sc > 0) ? sc : DEFAULT_CAPACITY; @SuppressWarnings("unchecked") Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n]; table = tab = nt; sc = n - (n >>> 2); } } finally { sizeCtl = sc; } break; } } return tab; }

initTable判断sizeCtl值,如果sizeCtl为-1表示有其他线程正在初始化数组,当前线程调用Thread.yield让出CPU。而正在初始化数组的线程通过Unsafe.compareAndSwapInt方法将sizeCtl改成-1。

initTable最外层一直使用while循环,而非if条件判断,就是确保数组可以初始化成功。

数组初始化成功之后,再执行添加的操作,调用tableAt通过volatile的方式找到(n-1)&hash处的bin节点。

Insertion (via put or its variants) of the first node in an empty bin is performed by just CASing it to the bin. This is by far the most common case for put operations under most key/hash distributions. Other update operations (insert, delete, and replace) require locks. We do not want to waste the space required to associate a distinct lock object with each bin, so instead use the first node of a bin list itself as a lock. Locking support for these locks relies on builtin "synchronized" monitors.

如果f的hash值为-1,说明当前f是ForwaringNode节点,意味着有其它线程正在扩容,则一起进行扩容操作。

完成添加或者更新操作之后,才执行break终止最外层没有终止条件的for循环,确保数据可以添加成功。

最后执行addCount方法。

private final void addCount(long x, int check) { CounterCell[] as; long b, s; // 利用CAS更新baseCoount if ((as = counterCells) != null || !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b x)) { CounterCell a; long v; int m; boolean uncontended = true; if (as == null || (m = as.length - 1) < 0 || (a = as[ThreadLocalRandom.getProbe() & m]) == null || !(uncontended = U.compareAndSwapLong(a, CELLVALUE, v = a.value, v x))) { fullAddCount(x, uncontended); return; } if (check <= 1) return; s = sumCount(); } // check >= 0,需要检查是否需要进行扩容操作 if (check >= 0) { Node<K,V>[] tab, nt; int n, sc; while (s >= (long)(sc = sizeCtl) && (tab = table) != null && (n = tab.length) < MAXIMUM_CAPACITY) { int rs = resizeStamp(n); if (sc < 0) { if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs 1 || sc == rs MAX_RESIZERS || (nt = nextTable) == null || transferIndex <= 0) break; if (U.compareAndSwapInt(this, SIZECTL, sc, sc 1)) transfer(tab, nt); } else if (U.compareAndSwapInt(this, SIZECTL, sc, (rs << RESIZE_STAMP_SHIFT) 2)) transfer(tab, null); s = sumCount(); } } }

扩容transfer什么时候会扩容

*插入一个新的节点:

不使用加锁的,支持多线程扩容。利用并发处理减少扩容带来性能的影响。

private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) { int n = tab.length, stride; if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE) stride = MIN_TRANSFER_STRIDE; // subdivide range if (nextTab == null) { // initiating try { @SuppressWarnings("unchecked") // 创建nextTab,容量为原来容量的两倍 Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1]; nextTab = nt; } catch (Throwable ex) { // try to cope with OOME // 扩容是抛出异常,将阈值设置成最大,表示不再扩容。 sizeCtl = Integer.MAX_VALUE; return; } nextTable = nextTab; transferIndex = n; } int nextn = nextTab.length; // 创建 ForwardingNode 节点,作为标记位,表明当前位置已经做过桶处理 ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab); // advance = true 表明该节点已经处理过了 boolean advance = true; boolean finishing = false; // to ensure sweep before committing nextTab for (int i = 0, bound = 0;;) { Node<K,V> f; int fh; // 控制 --i,遍历原hash表中的节点 while (advance) { int nextIndex, nextBound; if (--i >= bound || finishing) advance = false; else if ((nextIndex = transferIndex) <= 0) { i = -1; advance = false; } // 用CAS计算得到的transferIndex else if (U.compareAndSwapInt (this, TRANSFERINDEX, nextIndex, nextBound = (nextIndex > stride ? nextIndex - stride : 0))) { bound = nextBound; i = nextIndex - 1; advance = false; } } // 将原数组中的节点赋值到新的数组中,nextTab赋值给table,清空nextTable。 if (i < 0 || i >= n || i n >= nextn) { int sc; // 所有节点完成复制工作, if (finishing) { nextTable = null; table = nextTab; // 设置新的阈值为原来的1.5倍 sizeCtl = (n << 1) - (n >>> 1); return; } // 利用CAS方法更新扩容的阈值,sizeCtl减一,说明新加入一个线程参与到扩容中 if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) { if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT) return; finishing = advance = true; i = n; // recheck before commit } } // 遍历的节点为null,则放入到ForwardingNode指针节点 else if ((f = tabAt(tab, i)) == null) advance = casTabAt(tab, i, null, fwd); // f.hash==-1表示遍历到ForwardingNode节点,说明该节点已经处理过了 else if ((fh = f.hash) == MOVED) advance = true; // already processed else { // 节点加锁 synchronized (f) { if (tabAt(tab, i) == f) { Node<K,V> ln, hn; // fh>=0,表示为链表节点 if (fh >= 0) { // 构建两个链表,一个是原链表,另一个是原链表的反序链表 int runBit = fh & n; Node<K,V> lastRun = f; for (Node<K,V> p = f.next; p != null; p = p.next) { int b = p.hash & n; if (b != runBit) { runBit = b; lastRun = p; } } if (runBit == 0) { ln = lastRun; hn = null; } else { hn = lastRun; ln = null; } for (Node<K,V> p = f; p != lastRun; p = p.next) { int ph = p.hash; K pk = p.key; V pv = p.val; if ((ph & n) == 0) ln = new Node<K,V>(ph, pk, pv, ln); else hn = new Node<K,V>(ph, pk, pv, hn); } // 在nextTable i 位置处插入链表 setTabAt(nextTab, i, ln); // 在nextTable i n 位置处插入链表 setTabAt(nextTab, i n, hn); // 在table i的位置处插上ForwardingNode,表示该节点已经处理过 setTabAt(tab, i, fwd); // 可以执行 --i的操作,再次遍历节点 advance = true; } // TreeBin红黑树,按照红黑树处理,处理逻辑和链表类似 else if (f instanceof TreeBin) { TreeBin<K,V> t = (TreeBin<K,V>)f; TreeNode<K,V> lo = null, loTail = null; TreeNode<K,V> hi = null, hiTail = null; int lc = 0, hc = 0; for (Node<K,V> e = t.first; e != null; e = e.next) { int h = e.hash; TreeNode<K,V> p = new TreeNode<K,V> (h, e.key, e.val, null, null); if ((h & n) == 0) { if ((p.prev = loTail) == null) lo = p; else loTail.next = p; loTail = p; lc; } else { if ((p.prev = hiTail) == null) hi = p; else hiTail.next = p; hiTail = p; hc; } } // 扩容后树节点的个数<=6,红黑树转成链表 ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) : (hc != 0) ? new TreeBin<K,V>(lo) : t; hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) : (lc != 0) ? new TreeBin<K,V>(hi) : t; setTabAt(nextTab, i, ln); setTabAt(nextTab, i n, hn); setTabAt(tab, i, fwd); advance = true; } } } } } }

扩容过程有的复杂,主要涉及到多线程的并发扩容,ForwardingNode的作用就是支持扩容操作,将已经处理过的节点和空节点置为ForwardingNode,并发处理时多个线程处理ForwardingNode表示已经处理过了,就往后遍历。

总结,