一 put
final V putVal(K key, V value, boolean onlyIfAbsent) { if (key == null || value == null) throw new NullPointerException();//注意 和hashamp不同,concurrenthashmap可不能put key为null int hash = spread(key.hashCode()); int binCount = 0; for (Node<K,V>[] tab = table;;) {//concurrenthashmap对数据的操作大多都是自旋的方式,而不是像hashmap那样,一次操作 Node<K,V> f; int n, i, fh; if (tab == null || (n = tab.length) == 0) tab = initTable();//初始化,然后再次自旋的时候就能往下执行了 注意哦 ConcurrentHashmap可是在第一次putVal的时候初始化的 else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {//桶是null直接cas if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value, null))) break; // no lock when adding to empty bin } else if ((fh = f.hash) == MOVED) tab = helpTransfer(tab, f);//帮着扩容,注意helpTransfer是有返回值的,返回的是扩容后的数组的引用。这样下一次自旋就可以把value放进去了 else { V oldVal = null; synchronized (f) { if (tabAt(tab, i) == f) {//进来之后第一件事 就是去判断f变没变 因为有可能f已经被删了 if (fh >= 0) { binCount = 1; for (Node<K,V> e = f;; ++binCount) { K ek; 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; } } } 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)//如果链表大于等于8 尝试树化 treeifyBin(tab, i); if (oldVal != null) return oldVal; break; } } } addCount(1L, binCount);//元素数量加1 return null; }
整个put方法最值得注意的是,扩容。执行过helpTransfer以后,能够直接拿到扩容后的table的引用,自旋的下一次put也是往这个新的table里put。
二 remove
public V remove(Object key) { return replaceNode(key, null, null); }
final V replaceNode(Object key, V value, Object cv) {//value不为null就表示这是替换操作 int hash = spread(key.hashCode()); for (Node<K,V>[] tab = table;;) { Node<K,V> f; int n, i, fh; if (tab == null || (n = tab.length) == 0 || (f = tabAt(tab, i = (n - 1) & hash)) == null) break; else if ((fh = f.hash) == MOVED)//删除操作也会帮忙扩容,也是返回新的table,下一次自旋就能够在新的table里删除了 tab = helpTransfer(tab, f); else { V oldVal = null; boolean validated = false; synchronized (f) { if (tabAt(tab, i) == f) { if (fh >= 0) { validated = true; for (Node<K,V> e = f, pred = null;;) { K ek; if (e.hash == hash && ((ek = e.key) == key || (ek != null && key.equals(ek)))) { V ev = e.val; if (cv == null || cv == ev || (ev != null && cv.equals(ev))) { oldVal = ev; if (value != null)//value不为null表示这是替换 e.val = value; else if (pred != null)//表示该节点不是数组里的节点 pred.next = e.next; else setTabAt(tab, i, e.next);//数组里的cas替换掉 } break; } pred = e; if ((e = e.next) == null) break; } } else if (f instanceof TreeBin) { validated = true; TreeBin<K,V> t = (TreeBin<K,V>)f; TreeNode<K,V> r, p; if ((r = t.root) != null && (p = r.findTreeNode(hash, key, null)) != null) { V pv = p.val; if (cv == null || cv == pv || (pv != null && cv.equals(pv))) { oldVal = pv; if (value != null) p.val = value; else if (t.removeTreeNode(p))//删除树节点返回true表示要结束树化,退化成链表 setTabAt(tab, i, untreeify(t.first)); } } } } } if (validated) { if (oldVal != null) {//说明找到了 if (value == null)//说明这是删除,不是替换 addCount(-1L, -1); return oldVal; } break; } } } return null; }