put逻辑
//onlyIfAbsent 默认传的false
final V putVal(K key, V value, boolean onlyIfAbsent) {
//如果put的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;
//1. 数组为空 需要初始化
if (tab == null || (n = tab.length) == 0)
//初始化
tab = initTable();
//2. 如果不为空。如果该位置没有元素那么开始用cas去修改这个值(n - 1) & hash为定位位置元素
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
//采用cas添加一个节点 next节点域为空
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
//3. 如果检测到某个节点的hash值是MOVED,则表示正在进行数组扩张的数据复制阶段,则当前线程也会参与去复制,通过允许多线程复制的功能,一次来减少数组的复制所带来的性能损失
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
//4. 如果该位置有元素的话,那么开始加锁
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, 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)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
addCount(1L, binCount);
return null;
}
// 该方法是高位右移16位然后一自己异或最后再&HASH_BITS 目的是减少hash冲突的概率
static final int spread(int h) {
return (h ^ (h >>> 16)) & HASH_BITS;
}
初始化桶
public ConcurrentHashMap(int initialCapacity) {
if (initialCapacity < 0)
throw new IllegalArgumentException();
int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
MAXIMUM_CAPACITY :
tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
//数组大小
this.sizeCtl = cap;
}
//返回一个Node<K, V> 类型的数组
private final Node<K,V>[] initTable() {
Node<K,V>[] tab; int sc;
//首先判断是否需要初始化
while ((tab = table) == null || tab.length == 0) {
//sc < 0 说明别的线程在初始化
if ((sc = sizeCtl) < 0)
//把当前线程挂起
Thread.yield(); // lost initialization race; just spin
//unsafe 这个类调用c++实现的cas方法 SIZECTL表示偏移量 sc表示期待的值 -1表示要替换的值 如果替换成功这个线程开始初始化数组
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 = 3/4 n
sc = n - (n >>> 2);
}
} finally {
//初始化为原来的3/4
sizeCtl = sc;
}
break;
}
}
return tab;
}
扩容逻辑
private final void treeifyBin(Node<K,V>[] tab, int index) {
Node<K,V> b; int n, sc;
if (tab != null) {
if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
tryPresize(n << 1);
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
synchronized (b) {
if (tabAt(tab, index) == b) {
TreeNode<K,V> hd = null, tl = null;
for (Node<K,V> e = b; e != null; e = e.next) {
TreeNode<K,V> p =
new TreeNode<K,V>(e.hash, e.key, e.val,
null, null);
if ((p.prev = tl) == null)
hd = p;
else
tl.next = p;
tl = p;
}
setTabAt(tab, index, new TreeBin<K,V>(hd));
}
}
}
}
}