關(guān)于ThreadLocal以及InheritedThreadLocal基本原理的介紹已經(jīng)非常多踩叭,但是感覺threadlocal設(shè)計(jì)的精髓還是在于ThreadLocalMap,所以這里詳細(xì)分析一下ThreadLocalMap的實(shí)現(xiàn)。
成員變量
整體的成員變量和HashMap差不多指攒,主要不同就是擴(kuò)容閾值是2/3而非0.75秉氧,Entry的key是WeakReference<ThreadLocal<?>>。
//初始容量16
private static final int INITIAL_CAPACITY = 16;
//Entry數(shù)組
private Entry[] table;
//容量
private int size = 0;
//擴(kuò)容閾值
private int threshold; // Default to 0
//擴(kuò)容閾值固定為Entry數(shù)組長度的2/3演训,比HashMap要低冗懦,因?yàn)門hreadLocalMap使用開放定址-線性探測法處理hash沖突,Entry占用更快
private void setThreshold(int len) {
threshold = len * 2 / 3;
}
初始化
//首次調(diào)用ThreadLocal的set方法時仇祭,ThreadLocalMap會懶加載披蕉,存入一個元素,初始容量置1
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}
//Thread初始化乌奇,InheritedThreadLocal不為空時没讲,會調(diào)用該構(gòu)造函數(shù)創(chuàng)建子線程InheritedThreadLocalMap
private ThreadLocalMap(ThreadLocalMap parentMap) {
Entry[] parentTable = parentMap.table;
int len = parentTable.length;
setThreshold(len);
table = new Entry[len];
for (int j = 0; j < len; j++) {
Entry e = parentTable[j];
if (e != null) {
@SuppressWarnings("unchecked")
ThreadLocal<Object> key = (ThreadLocal<Object>) e.get();
if (key != null) {
Object value = key.childValue(e.value);
Entry c = new Entry(key, value);
int h = key.threadLocalHashCode & (len - 1);
while (table[h] != null)
h = nextIndex(h, len);
table[h] = c;
size++;
}
}
}
}
查詢、插入礁苗、刪除
和HashMap的實(shí)現(xiàn)有很大不同爬凑,hashkey沖突時采用線性地址法而非鏈地址法,并且在所有可能的地方都做了過期Entry的回收工作试伙,并對因?yàn)閔ash沖突而偏移的Entry進(jìn)行整理嘁信。
//這里分兩種情況處理,一種是e不為空且key相等疏叨,直接返回結(jié)果潘靖,另一種調(diào)用getEntryAfterMiss
private Entry getEntry(ThreadLocal<?> key) {
int i = key.threadLocalHashCode & (table.length - 1);
Entry e = table[i];
if (e != null && e.get() == key)
return e;
else
return getEntryAfterMiss(key, i, e);
}
//如果在hash槽位沒有找到該節(jié)點(diǎn),說名不存在或者沖突后偏移了蚤蔓,因此需要向后順序搜索卦溢,知道出現(xiàn)空槽位為止(空槽為說明后面不可能再存在了)。同時還會順便做Entry的清理工作
private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
Entry[] tab = table;
int len = tab.length;
while (e != null) {
ThreadLocal<?> k = e.get();
if (k == key)
return e;
if (k == null)
expungeStaleEntry(i);
else
i = nextIndex(i, len);
e = tab[i];
}
return null;
}
//從hashcode位置開始尋找空位并調(diào)用replaceStaleEntry插入秀又,或者遇到相同key則更新并直接return
private void set(ThreadLocal<?> key, Object value) {
Entry[] tab = table;
int len = tab.length;
int i = key.threadLocalHashCode & (len-1);
for (Entry e = tab[i];
e != null;
//nextIndex向后喚醒搜索數(shù)組
e = tab[i = nextIndex(i, len)]) {
ThreadLocal<?> k = e.get();
if (k == key) {
e.value = value;
return;
}
//如果key為null单寂,就用新key、value覆蓋吐辙,同時清理舊數(shù)據(jù)
if (k == null) {
replaceStaleEntry(key, value, i);
return;
}
}
//如果執(zhí)行到這里宣决,說明i位置是空的,并且需要直接插入
tab[i] = new Entry(key, value);
int sz = ++size;
//進(jìn)行清理昏苏,如果沒有清理出空間尊沸,就判斷是否需要擴(kuò)容
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;
//由于GC會批量回收無效引用威沫,所以set()的循環(huán)中發(fā)現(xiàn)一個過期槽位,就意味著這個key前面也可能出現(xiàn)了新的過期槽位椒丧,所以向前搜索并記錄可能存在的可用槽位壹甥。這樣可以增加空槽位的利用率,從而避免頻繁觸發(fā)rehash壶熏。
int slotToExpunge = staleSlot;
for (int i = prevIndex(staleSlot, len);
(e = tab[i]) != null;
i = prevIndex(i, len))
if (e.get() == null)
slotToExpunge = i;
//向后搜索句柠,直到null Entry或者有重復(fù)key
for (int i = nextIndex(staleSlot, len);
(e = tab[i]) != null;
i = nextIndex(i, len)) {
ThreadLocal<?> k = e.get();
//如果發(fā)現(xiàn)重復(fù)key,就和過期槽位做替換棒假,從而維持hashtable的順序性(每個entry離散列位置盡可能更近)
if (k == key) {
e.value = value;
tab[i] = tab[staleSlot];
tab[staleSlot] = e;
// 從i位置或者slotToExpunge位置進(jìn)行批量清理
if (slotToExpunge == staleSlot)
slotToExpunge = i;
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
return;
}
// 如果既沒有向前搜索到過期槽位溯职,也沒有向后搜索到重復(fù)key
if (k == null && slotToExpunge == staleSlot)
slotToExpunge = i;
}
// 如果沒有找到重復(fù)key,就直接替換過期鍵
tab[staleSlot].value = null;
tab[staleSlot] = new Entry(key, value);
// 如果有其它過期鍵帽哑,就進(jìn)行批量清理操作
if (slotToExpunge != staleSlot)
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
}
//移除操作谜酒,計(jì)算hashcode,然后從該位置向后遍歷直到遇到null槽位妻枕,逐個比較key值是否匹配僻族,如果匹配就調(diào)用Entry的clear方法,并從散列位置清理舊數(shù)據(jù)
private void remove(ThreadLocal<?> key) {
Entry[] tab = table;
int len = tab.length;
int i = key.threadLocalHashCode & (len-1);
for (Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
if (e.get() == key) {
e.clear();
expungeStaleEntry(i);
return;
}
}
}
值得注意的是這里調(diào)用了key.threadLocalHashCode獲取了ThreadLocal對象的hashcode屡谐,而其hashcode是通過靜態(tài)AtomicInteger以HASH_INCREMENT步長遞增生成的的述么。
private final int threadLocalHashCode = nextHashCode();
private static AtomicInteger nextHashCode =
new AtomicInteger();
private static final int HASH_INCREMENT = 0x61c88647;
private static int nextHashCode() {
return nextHashCode.getAndAdd(HASH_INCREMENT);
}
實(shí)際上這里使用的是斐波那契散列法,這是一種乘數(shù)散列法愕掏,只不過這個乘數(shù)比較特殊度秘,是32位整型上限2^32-1乘以黃金分割比例0.618....的值2654435769,用有符號整型表示就是-1640531527饵撑,去掉符號后16進(jìn)制表示為0x61c88647剑梳。使用0x61c88647為步長進(jìn)行散列,結(jié)果會更均勻滑潘。
清理
清理過程是基于熱度衰減的方式垢乙,從指定位置向后每搜索一位熱度減半,發(fā)現(xiàn)過期鍵則重置熱度众羡,直到熱度歸0結(jié)束清理侨赡。Redis掃描回收過期鍵時也與此比較類似,不過是按比例進(jìn)行采樣回收粱侣,直到采樣中過期鍵比例小于某個閾值。
//以i為起點(diǎn)向后搜索log2(n)位蓖宦,如果發(fā)現(xiàn)過期Entry則置n為len齐婴。換個說法就是先做小范圍掃描,如果發(fā)現(xiàn)過期Entry就做大范圍掃描稠茂,直到大范圍搜索也找不到過期Entry為止
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) {
n = len;
removed = true;
i = expungeStaleEntry(i);
}
} while ( (n >>>= 1) != 0);
return removed;
}
private int expungeStaleEntry(int staleSlot) {
Entry[] tab = table;
int len = tab.length;
//回收staleSlot槽位
tab[staleSlot].value = null;
tab[staleSlot] = null;
size--;
//對后續(xù)位置rehash柠偶,直到遇到null槽位
Entry e;
int i;
for (i = nextIndex(staleSlot, len);
(e = tab[i]) != null;
i = nextIndex(i, len)) {
ThreadLocal<?> k = e.get();
//如果可以清理情妖,就清理
if (k == null) {
e.value = null;
tab[i] = null;
size--;
} else {
//計(jì)算hashSlot位置
int h = k.threadLocalHashCode & (len - 1);
//如果entry沒有在hashSlot位置,則從hashSlot開始搜索空槽位并插入(相當(dāng)于整理诱担,避免沖突后entry偏離原位置過遠(yuǎn))
if (h != i) {
tab[i] = null;
while (tab[h] != null)
h = nextIndex(h, len);
tab[h] = e;
}
}
}
return i;
}
擴(kuò)容
和HashMap邏輯基本一致毡证,擴(kuò)容一倍并重新計(jì)算Hashslot
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();
//Entry過期,直接清理
if (k == null) {
e.value = null;
} else {
int h = k.threadLocalHashCode & (newLen - 1);
while (newTab[h] != null)
h = nextIndex(h, newLen);
newTab[h] = e;
count++;
}
}
}
setThreshold(newLen);
size = count;
table = newTab;
}