ConcurrentHashMap底层分析
初始化
private final Node<K,V>[] initTable() {
Node<K,V>[] tab; int sc;
while ((tab = table) == null || tab.length == 0) {
if ((sc = sizeCtl) < 0)
Thread.yield();
else if (U.compareAndSetInt(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;
}
- 由源码可以知道sizeCtl标识符是起到至关重要的作用的。接下来我们分析sizeCtl的三种情况。
- sizeCtl == 0时表示Node<K, V>数组还没有进行初始化。这时需要对其进行初始化,并且将SIZECTL设置为-1,表示正在进行初始化。其中设置为-1的过程是通过CAS的方式确保线程安全的。而进行初始化时,初始化容量为16。这时sizeCtl标识符的含义就变成扩容容量的阈值12(也就是16*0.75)。
- sizeCtl < 0: 如果此时sc=sizeCtl<0时有两种含义:①为-1时表示有一个线程正在初始化该数组。②小于-1时表示有n个线程正在对该数组进行扩容。当sc小于零时该线程应该让出CPU的控制权。
- sizeCtl>0: 大于0时则表示扩容的阈值
Put操作(对源码的逐步分析)
public V put(K key, V value) {
return putVal(key, value, false);
}
final V putVal(K key, V value, boolean onlyIfAbsent) {
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; K fk; V fv;
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)))
break;
}
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else if (onlyIfAbsent
&& fh == hash
&& ((fk = f.key) == key || (fk != null && key.equals(fk)))
&& (fv = f.val) != null)
return fv;
else {
V oldVal = null;
synchronized (f) {
if (tabAt(tab, i) == 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);
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;
}
}
else if (f instanceof ReservationNode)
throw new IllegalStateException("Recursive update");
}
}
if (binCount != 0) {
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
addCount(1L, binCount);
return null;
}
部分变量含义
- binCount:后面用来表示如果是链表的话插入k-v之后链表长度是否超过8
- f:表示目标位置元素
- fh:表示存放目标位置的元素hash值
关键步骤:
- 如果目标元素位置为空,则通过CAS放入,失败的话自旋确保成功。这个过程是不加锁的。
- 不为空时使用synchronized关键字对该Node加锁加入节点。如果是链表的话,
如果原k存在则会覆盖到原来的value并返回。如果没有原k返回null.跳出循环之后会对该链表进行判断,是否需要转换为红黑树。 - 否则是进行红黑树加入操作。
get操作
public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
int h = spread(key.hashCode());
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;
}
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
while ((e = e.next) != null) {
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}
比较简单直接步骤
- 计算hash值
- 找到指定位置判断头节点是否是要找的k值,是的话直接返回
- eh(头节点)小于0表示正在扩容或者红黑树
- 链表的话遍历查找