ConcurrentHashMap的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()); //@1,讲解见下面小标题。
//i处结点数量,2: TreeBin或链表结点数, 其它:链表结点数。主要用于每次加入结点后查看是否要由链表转为红黑树
int binCount = 0;
for (Node<K,V>[] tab = table;;) {
//CAS经典写法,不成功无限重试,让再次进行循环进行相应操作。
Node<K,V> f; int n, i, fh;
//除非构造时指定初始化集合,否则默认构造不初始化table,所以需要在添加时元素检查是否需要初始化。
if (tab == null || (n = tab.length) == 0)
tab = initTable(); //@2
//CAS操作得到对应table中元素
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
//@3
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; //null创建Node对象做为链表首结点
}
else if ((fh = f.hash) == MOVED) //当前结点正在扩容
//让当前线程调用helpTransfer也参与到扩容过程中来,扩容完毕后tab指向新table。
tab = helpTransfer(tab, f);
else {
V oldVal = null;
synchronized (f) {
if (tabAt(tab, i) == f) {
//双重检查i处结点未变化
if (fh >= 0) {
//表明是链表结点类型,hash值是大于0的,即spread()方法计算而来
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;
//onlyIfAbsent表示是新元素才加入,旧值不替换,默认为fase。
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K,V> pred = e;
if ((e = e.next) == null) {
//jdk1.8版本是把新结点加入链表尾部,next由volatile修饰
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) {
//@4