ThreadLocal
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
//map不为null,之前设置过情况
map.set(this, value);
else
createMap(t, value);
}
private void set(ThreadLocal<?> key, Object value) {
// We don't use a fast path as with get() because it is at
// least as common to use set() to create new entries as
// it is to replace existing ones, in which case, a fast
// path would fail more often than not.
Entry[] tab = table;
int len = tab.length;
//threadLocal对象的hash值&桶大小对应索引
int i = key.threadLocalHashCode & (len-1);
//索引下探,每次获取entry不为空,匹配地址相等情况
for (Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
ThreadLocal<?> k = e.get();
//如果之前这个线程设置过threadlocal对象的值
//并且地址相等情况就会替换
if (k == key) {
e.value = value;
return;
}
//弱引用key被gc清除或者其他情况,但是entry不为null
if (k == null) {
replaceStaleEntry(key, value, i);
return;
}
}
//如果e为空,直接赋值
tab[i] = new Entry(key, value);
int sz = ++size;
//向下扫码移除key=null,并判断是否需要扩容,清除失败,并且,size>=扩容阈值
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}
private void replaceStaleEntry(ThreadLocal<?> key, Object value,
int staleSlot) {
Entry[] tab = table;
int len = tab.length;
Entry e;
// Back up to check for prior stale entry in current run.
// We clean out whole runs at a time to avoid continual
// incremental rehashing due to garbage collector freeing
// up refs in bunches (i.e., whenever the collector runs).
int slotToExpunge = staleSlot;
//向左探测
for (int i = prevIndex(staleSlot, len);
//控制条件
(e = tab[i]) != null;
i = prevIndex(i, len))
if (e.get() == null)
//默认是入参staleSlot
//向左遍历,找出entry=null之后
//第一个弱引用key=null,索引设置为slotToExpunge
//设置清除key=null起始点
slotToExpunge = i;
// Find either the key or trailing null slot of run, whichever
// occurs first
//向右探测
for (int i = nextIndex(staleSlot, len);
(e = tab[i]) != null;
i = nextIndex(i, len)) {
ThreadLocal<?> k = e.get();
// If we find key, then we need to swap it
// with the stale entry to maintain hash table order.
// The newly stale slot, or any other stale slot
// encountered above it, can then be sent to expungeStaleEntry
// to remove or rehash all of the other entries in run.
if (k == key) {
e.value = value;
//当前匹配k=key地址存入entry不为null,key=null的值,注意tab[i] = tab[staleSlot];为原key=null指向当前i索引地址
tab[i] = tab[staleSlot];
//入参staleSlot地址放入新创建entry,key匹配
tab[staleSlot] = e;
// Start expunge at preceding stale entry if it exists
//如果起始点为原入参点,现在被tab[staleSlot] = e;占,那么新的起始点就是i
if (slotToExpunge == staleSlot)
slotToExpunge = i;
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
return;
}
// If we didn't find stale entry on backward scan, the
// first stale entry seen while scanning for key is the
// first still present in the run.
//向右探测,entry=null前,是否还存在key=null的节点,如果存在并且slotToExpunge == staleSlot,
//开始清理初始点就为i,只会执行一次
if (k == null && slotToExpunge == staleSlot)
slotToExpunge = i;
}
// If key not found, put new entry in stale slot
//同理占用
tab[staleSlot].value = null;
tab[staleSlot] = new Entry(key, value);
// If there are any other stale entries in run, expunge them
//使用向右探测清理初始点
if (slotToExpunge != staleSlot)
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
}
cleanSomeSlots判断是否存在需要清除,存在即扫描log2(n),给expungeStaleEntry清除快速返回下一个扫描entry!=null索引点
private boolean cleanSomeSlots(int i, int n) {
boolean removed = false;
Entry[] tab = table;
int len = tab.length;
do {
//从下一个索引开始
i = nextIndex(i, len);
Entry e = tab[i];
if (e != null && e.get() == null) {
//如果发现存在key=null,则n设置为len
//为了在while循环扫描控制:扫描log2(n)个单元格
n = len;
//remove标识
removed = true;
//返回tab[i]==null的索引,继续探测
i = expungeStaleEntry(i);
}
} while ( (n >>>= 1) != 0);
return removed;
}
expungeStaleEntry,向右清除缩小探测偏差,返回第一个entry=null索引
private int expungeStaleEntry(int staleSlot) {
Entry[] tab = table;
int len = tab.length;
// expunge entry at staleSlot
tab[staleSlot].value = null;
tab[staleSlot] = null;
size--;
// Rehash until we encounter null
Entry e;
int i;
//环形下探索引,直到tab[i]==null
for (i = nextIndex(staleSlot, len);
(e = tab[i]) != null;
i = nextIndex(i, len)) {
ThreadLocal<?> k = e.get();
//继续扫描存在key==null,清除
if (k == null) {
e.value = null;
tab[i] = null;
size--;
} else {
//如果key!=null,计算桶初始位置
int h = k.threadLocalHashCode & (len - 1);
//如果和初始位置不匹配
if (h != i) {
tab[i] = null;
// Unlike Knuth 6.4 Algorithm R, we must scan until
// null because multiple entries could have been stale.
//从初始h下探null桶放置,目的为了减少h索引偏差
while (tab[h] != null)
h = nextIndex(h, len);
tab[h] = e;
}
}
}
//返回tab[i]==null的索引
return i;
}
private void rehash() {
expungeStaleEntries();
//清除过后,还判断
// Use lower threshold for doubling to avoid hysteresis
//threshold = len * 2 / 3;
//size>=4/6*len-1/6*len
//也就是是否大于一半的len
if (size >= threshold - threshold / 4)
resize();
}
扩容过程中还会全清理,key=null节点
/**
* Expunge all stale entries in the table.
*/
private void expungeStaleEntries() {
Entry[] tab = table;
int len = tab.length;
for (int j = 0; j < len; j++) {
Entry e = tab[j];
if (e != null && e.get() == null)
expungeStaleEntry(j);
}
}
/**
* Double the capacity of the table.
*/
private void resize() {
Entry[] oldTab = table;
int oldLen = oldTab.length;
int newLen = oldLen * 2;
Entry[] newTab = new Entry[newLen];
int count = 0;
for (int j = 0; j < oldLen; ++j) {
Entry e = oldTab[j];
if (e != null) {
ThreadLocal<?> k = e.get();
//此处又置空key==null的entry
if (k == null) {
e.value = null; // Help the GC
} else {
//计算新的hash桶地址
int h = k.threadLocalHashCode & (newLen - 1);
//如果已经有值,继续下探
while (newTab[h] != null)
h = nextIndex(h, newLen);
newTab[h] = e;
count++;
}
}
}
//计算新的扩容阈值
setThreshold(newLen);
size = count;
table = newTab;
}
标签:解析,tab,int,staleSlot,len,ThreadLocal,源码,key,null
From: https://www.cnblogs.com/wsyphaha/p/18075860