ThreadLocal原理分析與程式碼驗證
ThreadLocal提供了執行緒安全的資料儲存和訪問方式,利用不帶key的get和set方法,居然能做到執行緒之間隔離,非常神奇。
比如
ThreadLocal<String> threadLocal = new ThreadLocal<>();
in thread 1
//in thread1
treadLocal.set("value1");
.....
//value的值是value1
String value = threadLocal.get();
in thread 2
//in thread2 treadLocal.set("value2"); ..... //value的值是value2 String value = threadLocal.get();
不論thread1和thread2是不是同時執行,都不會有執行緒安全問題,我們來測試一下。
執行緒安全測試
開10個執行緒,每個執行緒內都對同一個ThreadLocal物件set不同的值,會發現ThreadLocal在每個執行緒內部get出來的值,只會是自己執行緒內set進去的值,不會被別的執行緒影響。
static void testUsage() throws InterruptedException { Utils.println("-------------testUsage-------------------"); ThreadLocal<Long> threadLocal = new ThreadLocal<>(); AtomicBoolean threadSafe = new AtomicBoolean(true); int count = 10; CountDownLatch countDownLatch = new CountDownLatch(count); Random random = new Random(736832); for (int i = 0; i < count; i ++){ new Thread(() -> { try { //生成一個隨機數 Long value = System.nanoTime() + random.nextInt(); threadLocal.set(value); Thread.sleep(1000); Long value2 = threadLocal.get(); if (!value.equals(value2)) { //get和set的value不一致,說明被別的執行緒修改了,但這是不可能出現的 threadSafe.set(false); Utils.println("thread unsafe, this could not be happen!"); } } catch (InterruptedException e) { }finally { countDownLatch.countDown(); } }).start(); } countDownLatch.await(); Utils.println("all thread done, and threadSafe is " + threadSafe.get()); Utils.println("------------------------------------------"); }
輸出:
-------------testUsage------------------
all thread done, and threadSafe is true
-----------------------------------------
原理淺析
翻開ThreadLocal的原始碼,會發現ThreadLocal只是一個空殼子,它並不儲存具體的value,而是利用當前執行緒(Thread.currentThread())的threadLocalMap來儲存value,key就是這個threadLocal物件本身。
public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); } ThreadLocalMap getMap(Thread t) { return t.threadLocals; }
Thread的threadLocals欄位是ThreadLocalMap型別(你可以簡單理解為一個key value的Map),key是ThreadLocal物件,value是我們在外層設定的值
- 當我們呼叫threadLocal.set(value)方法的時候,會找到當前執行緒的threadLocals這個map,然後以this作為key去set key value
- 當我們呼叫threadLocal.get()方法的時候,會找到當前執行緒的threadLocals這個map,然後以this作為key去get value
- 當我們呼叫threadLocal.remove()方法的時候,會找到當前執行緒的threadLocals這個map,然後以this作為key去remove
這就相當於:
Thread.currentThread().threadLocals.set(threadLocal1, "value1");
.....
//value的值是value1
String value = Thread.currentThread().threadLocals.get(threadLocal1);
因為每個Thread都是不同的物件,所以他們的threadLocals也是不同的map,threadLocal在不同的執行緒裡工作時,實際上是從不同的map裡get/set,這也就是執行緒安全的原因了,瞭解到這一點就差不多了。
再深入一些,ThreadLocalMap的結構
如果繼續翻ThreadLocalMap的原始碼,會發現它有個欄位table,是Entry型別的陣列。
我們不妨寫段程式碼,把ThreadLocalMap的結構輸出出來。
由於Thread.threadLocals和ThreadLocalMap類不是public的,我們只有通過反射來獲取它的值。反射的程式碼如下(如果嫌長可以不看,直接看輸出):
static Object getThreadLocalMap(Thread thread) throws NoSuchFieldException, IllegalAccessException {
//get thread.threadLocals
Field threadLocals = Thread.class.getDeclaredField("threadLocals");
threadLocals.setAccessible(true);
return threadLocals.get(thread);
}
static void printThreadLocalMap(Object threadLocalMap) throws NoSuchFieldException, IllegalAccessException {
String threadName = Thread.currentThread().getName();
if(threadLocalMap == null){
Utils.println("threadMap is null, threadName:" + threadName);
return;
}
Utils.println(threadName);
//get threadLocalMap.table
Field tableField = threadLocalMap.getClass().getDeclaredField("table");
tableField.setAccessible(true);
Object[] table = (Object[])tableField.get(threadLocalMap);
Utils.println("----threadLocals (ThreadLocalMap), table.length = " + table.length);
for (int i = 0; i < table.length; i ++){
WeakReference<ThreadLocal<?>> entry = (WeakReference<ThreadLocal<?>>)table[i];
printEntry(entry, i);
}
}
static void printEntry(WeakReference<ThreadLocal<?>> entry, int i) throws NoSuchFieldException, IllegalAccessException {
if(entry == null){
Utils.println("--------table[" + i + "] -> null");
return;
}
ThreadLocal key = entry.get();
//get entry.value
Field valueField = entry.getClass().getDeclaredField("value");
valueField.setAccessible(true);
Object value = valueField.get(entry);
Utils.println("--------table[" + i + "] -> entry key = " + key + ", value = " + value);
}
測試程式碼:
static void testStructure() throws InterruptedException {
Utils.println("-------------testStructure----------------");
ThreadLocal<String> threadLocal1 = new ThreadLocal<>();
ThreadLocal<String> threadLocal2 = new ThreadLocal<>();
Thread thread1 = new Thread(() -> {
threadLocal1.set("threadLocal1-value");
threadLocal2.set("threadLocal2-value");
try {
Object threadLocalMap = getThreadLocalMap(Thread.currentThread());
printThreadLocalMap(threadLocalMap);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}, "thread1");
thread1.start();
//wait thread1 done
thread1.join();
Thread thread2 = new Thread(() -> {
threadLocal1.set("threadLocal1-value");
try {
Object threadLocalMap = getThreadLocalMap(Thread.currentThread());
printThreadLocalMap(threadLocalMap);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}, "thread2");
thread2.start();
thread2.join();
Utils.println("------------------------------------------");
}
我們在建立了兩個ThreadLocal的物件threadLocal1和threadLocal2,線上程1裡為這兩個物件設定值,線上程2裡只為threadLocal1設定值。然後分別打印出這兩個執行緒的threadLocalMap。
輸出結果為:
-------------testStructure----------------
thread1
----threadLocals (ThreadLocalMap), table.length = 16
--------table[0] -> null
--------table[1] -> entry key = java.lang.ThreadLocal@33baa315, value = threadLocal2-value
--------table[2] -> null
--------table[3] -> null
--------table[4] -> null
--------table[5] -> null
--------table[6] -> null
--------table[7] -> null
--------table[8] -> null
--------table[9] -> null
--------table[10] -> entry key = java.lang.ThreadLocal@4d42db5c, value = threadLocal1-value
--------table[11] -> null
--------table[12] -> null
--------table[13] -> null
--------table[14] -> null
--------table[15] -> null
thread2
----threadLocals (ThreadLocalMap), table.length = 16
--------table[0] -> null
--------table[1] -> null
--------table[2] -> null
--------table[3] -> null
--------table[4] -> null
--------table[5] -> null
--------table[6] -> null
--------table[7] -> null
--------table[8] -> null
--------table[9] -> null
--------table[10] -> entry key = java.lang.ThreadLocal@4d42db5c, value = threadLocal1-value
--------table[11] -> null
--------table[12] -> null
--------table[13] -> null
--------table[14] -> null
--------table[15] -> null
------------------------------------------
從結果上可以看出:
- 執行緒1和執行緒2的threadLocalMap物件的table欄位,是個陣列,長度都是16
- 由於執行緒1裡給兩個threadLocal物件設定了值,所以執行緒1的ThreadLocalMap裡有兩個entry,陣列下標分別是1和10,其餘的是null(如果你自己寫程式碼驗證,下標不一定是1和10,不需要糾結這個問題,只要前後對的上就行)
- 由於執行緒2裡只給一個threadLocal物件設定了值,所以執行緒1的ThreadLocalMap裡只有一個entry,陣列下標是10,其餘的是null
- threadLocal1這個物件在兩個執行緒裡都設定了值,所以當它作為key加入二者的threadLocalMap時,key是一樣的,都是java.lang.ThreadLocal@4d42db5c;下標也是一樣的,都是10。
為什麼是WeakReference
檢視Entry的原始碼,會發現Entry繼承自WeakReference:
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
建構函式裡把key傳給了super,也就是說,ThreadLocalMap中對key的引用,是WeakReference的。
Weak reference objects, which do not prevent their referents from being
made finalizable, finalized, and then reclaimed. Weak references are most
often used to implement canonicalizing mappings.
通俗點解釋:
當一個物件僅僅被weak reference(弱引用), 而沒有任何其他strong reference(強引用)的時候, 不論當前的記憶體空間是否足夠,當GC執行的時候, 這個物件就會被回收。
看不明白沒關係,還是寫程式碼測試一下什麼是WeakReference吧...
static void testWeakReference(){
Object obj1 = new Object();
Object obj2 = new Object();
WeakReference<Object> obj1WeakRef = new WeakReference<>(obj1);
WeakReference<Object> obj2WeakRf = new WeakReference<>(obj2);
//obj32StrongRef是強引用
Object obj2StrongRef = obj2;
Utils.println("before gc: obj1WeakRef = " + obj1WeakRef.get() + ", obj2WeakRef = " + obj2WeakRf.get() + ", obj2StrongRef = " + obj2StrongRef);
//把obj1和obj2設為null
obj1 = null;
obj2 = null;
//強制gc
forceGC();
Utils.println("after gc: obj1WeakRef = " + obj1WeakRef.get() + ", obj2WeakRef = " + obj2WeakRf.get() + ", obj2StrongRef = " + obj2StrongRef);
}
結果輸出:
before gc: obj1WeakRef = java.lang.Object@4554617c, obj2WeakRef = java.lang.Object@74a14482, obj2StrongRef = java.lang.Object@74a14482
after gc: obj1WeakRef = null, obj2WeakRef = java.lang.Object@74a14482, obj2StrongRef = java.lang.Object@74a14482
從結果上可以看出:
- 我們先new了兩個物件(為避免混淆,稱他們為Object1和Object2),分別用變數obj1和obj2指向它們,同時定義了一個obj2StrongRef,也指向Object2,最後把obj1和obj2均指向null
- 由於Object1沒有變數強引用它了,所以在gc後,Object1被回收了,obj1WeakRef.get()返回了null
- 由於Object2還有obj2StrongRef在引用它,所以gc後,Object2依然存在,沒有被回收。
那麼,ThreadLocalMap中對key的引用,為什麼是WeakReference的呢?
因為大部分情況下,執行緒不死
大部分情況下,執行緒不會頻繁的建立和銷燬,一般都會用執行緒池。所以執行緒物件一般不會被清除,執行緒的threadLocalMap就一直存在。
如果key對ThreadLocal是強引用,那麼key永遠不會被回收,即使我們程式裡再也不用它了。
但是key是弱引用的話,情況就會得到改善:只要沒有指向threadLocal的強引用了,這個ThreadLocal物件就會被清理。
我們還是寫程式碼測試一下吧。
/**
* 測試ThreadLocal物件什麼時候被回收
* @throws InterruptedException
*/
static void testGC() throws InterruptedException {
Utils.println("-----------------testGC-------------------");
Thread thread1 = new Thread(() -> {
ThreadLocal<String> threadLocal1 = new ThreadLocal<>();
ThreadLocal<String> threadLocal2 = new ThreadLocal<>();
threadLocal1.set("threadLocal1-value");
threadLocal2.set("threadLocal2-value");
try {
Object threadLocalMap = getThreadLocalMap(Thread.currentThread());
Utils.println("print threadLocalMap before gc");
printThreadLocalMap(threadLocalMap);
//set threadLocal1 unreachable
threadLocal1 = null;
forceGC();
Utils.println("print threadLocalMap after gc");
printThreadLocalMap(threadLocalMap);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}, "thread1");
thread1.start();
thread1.join();
Utils.println("------------------------------------------");
}
我們在一個執行緒裡為兩個ThreadLocal物件賦值,最後把其中一個物件的強引用移除,gc後列印當前執行緒的threadLocalMap。
輸出結果如下:
-----------------testGC-------------------
print threadLocalMap before gc
thread1
----threadLocals (ThreadLocalMap), table.length = 16
--------table[0] -> null
--------table[1] -> entry key = java.lang.ThreadLocal@7bf9cebf, value = threadLocal2-value
--------table[2] -> null
--------table[3] -> null
--------table[4] -> null
--------table[5] -> null
--------table[6] -> null
--------table[7] -> null
--------table[8] -> null
--------table[9] -> null
--------table[10] -> entry key = java.lang.ThreadLocal@56342d38, value = threadLocal1-value
--------table[11] -> null
--------table[12] -> null
--------table[13] -> null
--------table[14] -> null
--------table[15] -> null
print threadLocalMap after gc
thread1
----threadLocals (ThreadLocalMap), table.length = 16
--------table[0] -> null
--------table[1] -> entry key = java.lang.ThreadLocal@7bf9cebf, value = threadLocal2-value
--------table[2] -> null
--------table[3] -> null
--------table[4] -> null
--------table[5] -> null
--------table[6] -> null
--------table[7] -> null
--------table[8] -> null
--------table[9] -> null
--------table[10] -> entry key = null, value = threadLocal1-value
--------table[11] -> null
--------table[12] -> null
--------table[13] -> null
--------table[14] -> null
--------table[15] -> null
------------------------------------------
從輸出結果可以看到,當我們把threadLocal1的強引用移除並gc之後,table[10]的key變成了null,說明threadLocal1這個物件被回收了;threadLocal2的強引用還在,所以table[1]的key不是null,沒有被回收。
但是你發現沒有,table[10]的key雖然是null了,但value還活著! table[10]這個entry物件,也活著!
是的,因為只有key是WeakReference....
無用的entry什麼時候被回收?
通過檢視ThreadLocal的原始碼,發現在ThreadLocal物件的get/set/remove方法執行時,都有機會清除掉map中已經無用的entry。
最容易驗證清除無用entry的場景分別是:
- remove:這個不用說了,這哥們本來就是做這個的
- get:當一個新的threadLocal物件(沒有set過value)發生get呼叫時,也會作為新的entry加入map,在加入的過程中,有機會清除掉無用的entry,邏輯和下面的set相同。
- set: 當一個新的threadLocal物件(沒有set過value)發生set呼叫時,會在map中加入新的entry,此時有機會清除掉無用的entry,清除的邏輯是:
- 清除掉table陣列中的那些無用entry中的一部分,記住是一部分,這個一部分可能全部,也可能是0,具體演算法請看ThreadLocalMap.cleanSomeSlots,這裡不解釋了。
- 如果上一步的"一部分"是0(即清除了0個),並且map的size(是真實size,不是table.length)大於等於threshold(table.length的2/3),會執行一次rehash,在rehash的過程中,清理掉所有無用的entry,並減小size,清理後的size如果還大於等於threshold - threshold/4,則把table擴容為原來的兩倍大小。
還有其他場景,但不好驗證,這裡就不提了。
ThreadLocal原始碼就不貼了,貼了也講不明白,相關邏輯在setInitialValue、cleanSomeSlots、expungeStaleEntries、rehash、resize等方法裡。
在我們寫程式碼驗證entry回收邏輯之前,還需要簡單的提一下ThreadLocalMap的hash演算法。
entry陣列的下標如何確定?
每個ThreadLocal物件,都有一個threadLocalHashCode變數,在加入ThreadLocalMap的時候,根據這個threadLocalHashCode的值,對entry陣列的長度取餘(hash & (len - 1)),餘數作為下標。
那麼threadLocalHashCode是怎麼計算的呢?看原始碼:
public class ThreadLocal<T>{
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);
}
...
}
ThreadLocal類維護了一個全域性靜態欄位nextHashCode,每new一個ThreadLocal物件,nextHashCode都會遞增0x61c88647,作為下一個ThreadLocal物件的threadLocalHashCode。
這個0x61c88647,是個神奇的數字,只要以它為遞增值,那麼和2的N次方取餘時,在有限的次數內不會發生重複。
比如和16取餘,那麼在16次遞增內,不會發生重複。還是寫程式碼驗證一下吧。
int hashCode = 0;
int HASH_INCREMENT = 0x61c88647;
int length = 16;
for(int i = 0; i < length ; i ++){
int h = hashCode & (length - 1);
hashCode += HASH_INCREMENT;
System.out.println("h = " + h + ", i = " + i);
}
輸出結果為:
h = 0, i = 0
h = 7, i = 1
h = 14, i = 2
h = 5, i = 3
h = 12, i = 4
h = 3, i = 5
h = 10, i = 6
h = 1, i = 7
h = 8, i = 8
h = 15, i = 9
h = 6, i = 10
h = 13, i = 11
h = 4, i = 12
h = 11, i = 13
h = 2, i = 14
h = 9, i = 15
你看,h的值在16次遞增內,沒有發生重複。 但是要記住,2的N次方作為長度才會有這個效果,這也解釋了為什麼ThreadLocalMap的entry陣列初始長度是16,每次都是2倍的擴容。
驗證新threadLocal的get和set時回收部分無效的entry
為了驗證出結果,我們需要先給ThreadLocal的nextHashCode重置一個初始值,這樣在測試的時候,每個threadLocal的陣列下標才會按照我們設計的思路走。
static void resetNextHashCode() throws NoSuchFieldException, IllegalAccessException {
Field nextHashCodeField = ThreadLocal.class.getDeclaredField("nextHashCode");
nextHashCodeField.setAccessible(true);
nextHashCodeField.set(null, new AtomicInteger(1253254570));
}
然後在測試程式碼裡,我們先呼叫resetNextHashCode方法,然後加兩個ThreadLocal物件並set值,gc前把強引用去除,gc後再new兩個新的theadLocal物件,分別呼叫他們的get和set方法。
在每個關鍵點打印出threadLocalMap做比較。
static void testExpungeSomeEntriesWhenGetOrSet() throws InterruptedException {
Utils.println("----------testExpungeStaleEntries----------");
Thread thread1 = new Thread(() -> {
try {
resetNextHashCode();
//注意,這裡必須有兩個ThreadLocal,才能驗證出threadLocal1被清理
ThreadLocal<String> threadLocal1 = new ThreadLocal<>();
ThreadLocal<String> threadLocal2 = new ThreadLocal<>();
threadLocal1.set("threadLocal1-value");
threadLocal2.set("threadLocal2-value");
Object threadLocalMap = getThreadLocalMap(Thread.currentThread());
//set threadLocal1 unreachable
threadLocal1 = null;
threadLocal2 = null;
forceGC();
Utils.println("print threadLocalMap after gc");
printThreadLocalMap(threadLocalMap);
ThreadLocal<String> newThreadLocal1 = new ThreadLocal<>();
newThreadLocal1.get();
Utils.println("print threadLocalMap after call a new newThreadLocal1.get");
printThreadLocalMap(threadLocalMap);
ThreadLocal<String> newThreadLocal2 = new ThreadLocal<>();
newThreadLocal2.set("newThreadLocal2-value");
Utils.println("print threadLocalMap after call a new newThreadLocal2.set");
printThreadLocalMap(threadLocalMap);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}, "thread1");
thread1.start();
thread1.join();
Utils.println("------------------------------------------");
}
程式輸出結果為:
----------testExpungeStaleEntries----------
print threadLocalMap after gc
thread1
----threadLocals (ThreadLocalMap), table.length = 16
--------table[0] -> null
--------table[1] -> entry key = null, value = threadLocal2-value
--------table[2] -> null
--------table[3] -> null
--------table[4] -> null
--------table[5] -> null
--------table[6] -> null
--------table[7] -> null
--------table[8] -> null
--------table[9] -> null
--------table[10] -> entry key = null, value = threadLocal1-value
--------table[11] -> null
--------table[12] -> null
--------table[13] -> null
--------table[14] -> null
--------table[15] -> null
print threadLocalMap after call a new newThreadLocal1.get
thread1
----threadLocals (ThreadLocalMap), table.length = 16
--------table[0] -> null
--------table[1] -> entry key = null, value = threadLocal2-value
--------table[2] -> null
--------table[3] -> null
--------table[4] -> null
--------table[5] -> null
--------table[6] -> null
--------table[7] -> null
--------table[8] -> entry key = java.lang.ThreadLocal@2b63dc81, value = null
--------table[9] -> null
--------table[10] -> null
--------table[11] -> null
--------table[12] -> null
--------table[13] -> null
--------table[14] -> null
--------table[15] -> null
print threadLocalMap after call a new newThreadLocal2.set
thread1
----threadLocals (ThreadLocalMap), table.length = 16
--------table[0] -> null
--------table[1] -> null
--------table[2] -> null
--------table[3] -> null
--------table[4] -> null
--------table[5] -> null
--------table[6] -> null
--------table[7] -> null
--------table[8] -> entry key = java.lang.ThreadLocal@2b63dc81, value = null
--------table[9] -> null
--------table[10] -> null
--------table[11] -> null
--------table[12] -> null
--------table[13] -> null
--------table[14] -> null
--------table[15] -> entry key = java.lang.ThreadLocal@2e93c547, value = newThreadLocal2-value
------------------------------------------
從結果上來看,
- gc後table[1]和table[10]的key變成了null
- new newThreadLocal1.get後,新增了table[8],table[10]被清理了,但table[1]還在(這就是cleanSomeSlots中some的意思)
- new newThreadLocal2.set後,新增了table[15],table[1]被清理了。
驗證map的size大於等於table.length的2/3時回收所有無效的entry
static void testExpungeAllEntries() throws InterruptedException {
Utils.println("----------testExpungeStaleEntries----------");
Thread thread1 = new Thread(() -> {
try {
resetNextHashCode();
int threshold = 16 * 2 / 3;
ThreadLocal[] threadLocals = new ThreadLocal[threshold - 1];
for(int i = 0; i < threshold - 1; i ++){
threadLocals[i] = new ThreadLocal<String>();
threadLocals[i].set("threadLocal" + i + "-value");
}
Object threadLocalMap = getThreadLocalMap(Thread.currentThread());
threadLocals[1] = null;
threadLocals[8] = null;
//threadLocals[6] = null;
//threadLocals[4] = null;
//threadLocals[2] = null;
forceGC();
Utils.println("print threadLocalMap after gc");
printThreadLocalMap(threadLocalMap);
ThreadLocal<String> newThreadLocal1 = new ThreadLocal<>();
newThreadLocal1.set("newThreadLocal1-value");
Utils.println("print threadLocalMap after call a new newThreadLocal1.get");
printThreadLocalMap(threadLocalMap);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}, "thread1");
thread1.start();
thread1.join();
Utils.println("------------------------------------------");
}
我們先建立了9個threadLocal物件並設定了值,然後去掉了其中2個的強引用(注意這2個可不是隨意挑選的)。
gc後再新增一個新的threadLocal,最後打印出最新的map。輸出為:
----------testExpungeStaleEntries----------
print threadLocalMap after gc
thread1
----threadLocals (ThreadLocalMap), table.length = 16
--------table[0] -> null
--------table[1] -> entry key = null, value = threadLocal1-value
--------table[2] -> entry key = null, value = threadLocal8-value
--------table[3] -> null
--------table[4] -> entry key = java.lang.ThreadLocal@60523912, value = threadLocal6-value
--------table[5] -> null
--------table[6] -> entry key = java.lang.ThreadLocal@48fccd7a, value = threadLocal4-value
--------table[7] -> null
--------table[8] -> entry key = java.lang.ThreadLocal@188bbe72, value = threadLocal2-value
--------table[9] -> null
--------table[10] -> entry key = java.lang.ThreadLocal@19e0ebe8, value = threadLocal0-value
--------table[11] -> entry key = java.lang.ThreadLocal@688bcb6f, value = threadLocal7-value
--------table[12] -> null
--------table[13] -> entry key = java.lang.ThreadLocal@46324c19, value = threadLocal5-value
--------table[14] -> null
--------table[15] -> entry key = java.lang.ThreadLocal@38f1283, value = threadLocal3-value
print threadLocalMap after call a new newThreadLocal1.get
thread1
----threadLocals (ThreadLocalMap), table.length = 32
--------table[0] -> null
--------table[1] -> null
--------table[2] -> null
--------table[3] -> null
--------table[4] -> null
--------table[5] -> null
--------table[6] -> entry key = java.lang.ThreadLocal@48fccd7a, value = threadLocal4-value
--------table[7] -> null
--------table[8] -> null
--------table[9] -> entry key = java.lang.ThreadLocal@1dae16b1, value = newThreadLocal1-value
--------table[10] -> entry key = java.lang.ThreadLocal@19e0ebe8, value = threadLocal0-value
--------table[11] -> null
--------table[12] -> null
--------table[13] -> entry key = java.lang.ThreadLocal@46324c19, value = threadLocal5-value
--------table[14] -> null
--------table[15] -> null
--------table[16] -> null
--------table[17] -> null
--------table[18] -> null
--------table[19] -> null
--------table[20] -> entry key = java.lang.ThreadLocal@60523912, value = threadLocal6-value
--------table[21] -> null
--------table[22] -> null
--------table[23] -> null
--------table[24] -> entry key = java.lang.ThreadLocal@188bbe72, value = threadLocal2-value
--------table[25] -> null
--------table[26] -> null
--------table[27] -> entry key = java.lang.ThreadLocal@688bcb6f, value = threadLocal7-value
--------table[28] -> null
--------table[29] -> null
--------table[30] -> null
--------table[31] -> entry key = java.lang.ThreadLocal@38f1283, value = threadLocal3-value
------------------------------------------
從結果上看:
- gc後table[1]和table[2](即threadLocal1和threadLocal8)的key變成了null
- 加入新的threadLocal後,table的長度從16變成了32(因為此時的size是8,正好等於10 - 10/4,所以擴容),並且threadLocal1和threadLocal8這兩個entry不見了。
如果在gc前,我們把threadLocals[1、8、6、4、2]都去掉強引用,加入新threadLocal後會發現1、8、6、4、2被清除了,但沒有擴容,因為此時size是5,小於10-10/4。這個邏輯就不貼測試結果了,你可以取消註釋上面程式碼中相關的邏輯試試。
大部分場景下,ThreadLocal物件的生命週期是和app一致的,弱引用形同虛設
回到現實中。
我們用ThreadLocal的目的,無非是在跨方法呼叫時更方便的執行緒安全地儲存和使用變數。這就意味著ThreadLocal的生命週期很長,甚至和app是一起存活的,強引用一直在。
既然強引用一直存在,那麼弱引用就形同虛設了。
所以在確定不再需要ThreadLocal中的值的情況下,還是老老實實的呼叫remove方法吧!
程式碼地址
https://github.com/kongxiangxin/pine/tree/master/threadlo