table:默认为null,初始化发生在第一次插入操作,默认大小为16的数组,用来存储Node节点数据,扩容时大小总是2的幂次方。
nextTable:默认为null,扩容时新生成的数组,其大小为原数组的两倍。
sizeCtl :默认为0,用来控制table的初始化和扩容操作,具体应用在后续会体现出来。
-1 代表table正在初始化
N:代表正在由多少个正在resize的线程
当初始化之后 该值用于表示什么时候可以resize
锁只有在遇到散列冲突的时候才用
hash的四个值 moved 代表节点正在resize
TREEBIN代表该
static final int MOVED = -1; // hash for forwarding nodes
static final int TREEBIN = -2; // hash for roots of trees
static final int RESERVED = -3; // hash for transient reservations
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
final V putVal(K key, V value, boolean onlyIfAbsent) {
如果key和value出现null 抛出空指针
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;
如果table==null或者长度为0 初始化table
if (tab == null || (n = tab.length) == 0)
tab = initTable();
否则通过tabAt获取table里面的这个下标的元素,这是通过unsafe的getObjectVolatile方法获取最新的元素,虽然table是volatile,但是其元素不是,所以不通过这个方式我们有可能获取的还是每个线程的工作内存中的元素,而不是主内存中的真正元素
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
}
(fh = f.hash) == MOVED代表 ForwardingNode:一个特殊的Node节点,hash值为-1,其中存储nextTable的引用。
意味有其它线程正在扩容,则一起进行扩容操作。
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
代表这个时候下标已经有了元素,即有可能存在链表或者红黑树
V oldVal = null;
synchronized (f) {
加完锁后获取最新的节点元素与f节点对比
如果不等,说明再此期间有数据更改,直接不处理 等到下一次的loop
if (tabAt(tab, i) == f) {
fh大于0 代表该节点是正常的节点或者链表
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;
}
}
}
这边会对是否进行resize进判断
addCount(1L, binCount);
return null;
}
private final Node<K,V>[] initTable() {
Node<K,V>[] tab; int sc;
如果table为null 则初始化
while ((tab = table) == null || tab.length == 0) {
如果已经小于0了 则代表已经有线程初始化了 则 我们这边
让出cpu
if ((sc = sizeCtl) < 0)
Thread.yield(); // lost initialization race; just spin
设置为-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 = n - (n >>> 2);
}
} finally {
设置下次需要resize的值
sizeCtl = sc;
}
break;
}
}
return tab;
}
private final void addCount(long x, int check) {
CounterCell[] as; long b, s;
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();
}
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();
}
}
}
resize
final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
Node<K,V>[] nextTab; int sc;
if (tab != null && (f instanceof ForwardingNode) &&
(nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
int rs = resizeStamp(tab.length);
while (nextTab == nextTable && table == tab &&
(sc = sizeCtl) < 0) {
if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
sc == rs + MAX_RESIZERS || transferIndex <= 0)
break;
if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
transfer(tab, nextTab);
break;
}
}
return nextTab;
}
return table;
}
网友评论