java開發(fā)工作中對InheritableThreadLocal使用思考
引言
最近在工作中結(jié)合線程池使用 InheritableThreadLocal 出現(xiàn)了獲取線程變量“錯誤”的問題,看了相關(guān)的文檔和源碼后在此記錄。
1. 先說結(jié)論
InheritableThreadLocal 只有在父線程創(chuàng)建子線程時,在子線程中才能獲取到父線程中的線程變量;
當配合線程池使用時:“第一次在線程池中開啟線程,能在子線程中獲取到父線程的線程變量,而當該子線程開啟之后,發(fā)生線程復用,該子線程仍然保留的是之前開啟它的父線程的線程變量,而無法獲取當前父線程中新的線程變量”,所以會發(fā)生獲取線程變量錯誤的情況。
2. 實驗例子
- 創(chuàng)建一個線程數(shù)固定為1的線程池,先在main線程中存入變量1,并使用線程池開啟新的線程打印輸出線程變量,之后更改main線程的線程變量為變量2,再使用線程池中線程(發(fā)生線程復用)打印輸出線程變量,對比兩次輸出的值是否不同
/**
* 測試線程池下InheritableThreadLocal線程變量失效的場景
*/
public class TestInheritableThreadLocal {
private static final InheritableThreadLocal<String> threadLocal = new InheritableThreadLocal<>();
// 固定大小的線程池,保證線程復用
private static final ExecutorService executorService = Executors.newFixedThreadPool(1);
public static void main(String[] args) {
threadLocal.set("main線程 變量1");
// 正常取到 main線程 變量1
executorService.execute(() -> System.out.println(threadLocal.get()));
threadLocal.set("main線程 變量2");
// 線程復用再取還是 main線程 變量1
executorService.execute(() -> System.out.println(threadLocal.get()));
}
}
輸出結(jié)果:
main線程 變量1 main線程 變量1
發(fā)現(xiàn)兩次輸出結(jié)果值相同,證明發(fā)生線程復用時,子線程獲取父線程變量失效
3. 詳解
3.1 JavaDoc
This class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values. Normally the child's values will be identical to the parent's; however, the child's value can be made an arbitrary function of the parent's by overriding the childValue method in this class. Inheritable thread-local variables are used in preference to ordinary thread-local variables when the per-thread-attribute being maintained in the variable (e.g., User ID, Transaction ID) must be automatically transmitted to any child threads that are created.
InheritableThreadLocal 繼承了 ThreadLocal, 以能夠讓子線程能夠從父線程中繼承線程變量: 當一個子線程被創(chuàng)建時,它會接收到父線程中所有可繼承的變量。通常情況下,子線程和父線程中的線程變量是完全相同的,但是可以通過重寫 childValue 方法來使父子線程中的值不同。
當線程中維護的變量如UserId, TransactionId 等必須自動傳遞到新創(chuàng)建的任何子線程時,使用InheritableThreadLocal要優(yōu)于ThreadLocal
3.2 源碼
public class InheritableThreadLocal<T> extends ThreadLocal<T> {
/**
* 當子線程被創(chuàng)建時,通過該方法來初始化子線程中線程變量的值,
* 這個方法在父線程中被調(diào)用,并且在子線程開啟之前。
*
* 通過重寫這個方法可以改變從父線程中繼承過來的值。
*
* @param parentValue the parent thread's value
* @return the child thread's initial value
*/
protected T childValue(T parentValue) {
return parentValue;
}
ThreadLocalMap getMap(Thread t) {
return t.inheritableThreadLocals;
}
void createMap(Thread t, T firstValue) {
t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);
}
}
其中childValue方法來獲取父線程中的線程變量的值,也可通過重寫這個方法來將獲取到的線程變量的值進行修改。
在getMap方法和createMap方法中,可以發(fā)現(xiàn)inheritableThreadLocals變量,它是 ThreadLocalMap,在Thread類中

3.2.1 childValue方法
- 開啟新線程時,會調(diào)用Thread的構(gòu)造方法
public Thread(ThreadGroup group, String name) {
init(group, null, name, 0);
}
- 沿著構(gòu)造方法向下,找到
init方法的最終實現(xiàn),其中有如下邏輯:為當前線程創(chuàng)建線程變量以繼承父線程中的線程變量
/**
* @param inheritThreadLocals 為ture,代表是為 包含可繼承的線程變量 的線程進行初始化
*/
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
...
if (inheritThreadLocals && parent.inheritableThreadLocals != null)
// 注意這里創(chuàng)建子線程的線程變量
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
...
}
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals)創(chuàng)建子線程 InheritedMap 的具體實現(xiàn)
createInheritedMap 方法,最終會調(diào)用到 ThreadLocalMap 的私有構(gòu)造方法,傳入的參數(shù)parentMap即為父線程中保存的線程變量
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) {
// 注意?。?! 這里調(diào)用了childValue方法
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++;
}
}
}
}
這個方法會對父線程中的線程變量做深拷貝,其中調(diào)用了childValue方法來獲取/初始化子線程中的值,并保存到子線程中
- 由上可見,可繼承的線程變量只是在線程被創(chuàng)建的時候進行了初始化工作,這也就能解釋為什么在線程池中發(fā)生線程復用時不能獲取到父線程線程變量的原因
4. 實驗例子流程圖

- main線程set main線程 變量1時,會調(diào)用到
InheritableThreadLocal的createMap方法,創(chuàng)建inheritableThreadLocals并保存線程變量 - 開啟子線程1時,會深拷貝父線程中的線程變量到子線程中,如圖示
- main線程set main線程 變量2,會覆蓋主線程中之前set的mian線程變量1
- 最后發(fā)生線程復用,子線程1無法獲取到main線程新set的值,仍然打印 main線程 變量1
以上就是java開發(fā)工作中對InheritableThreadLocal使用思考的詳細內(nèi)容,更多關(guān)于java開發(fā)InheritableThreadLocal的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringCloud使用Feign實現(xiàn)服務調(diào)用
這篇文章主要為大家詳細介紹了SpringCloud使用Feign實現(xiàn)服務調(diào)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
Spring?Boot中application配置文件的生效順序及應用范圍
Spring?Boot的一個重要特性就是它的自動配置,這一特性在很大程度上依賴于名稱為application的配置文件,本文將詳細介紹在Spring?Boot中,這些配置文件的加載順序以及每份文件的應用范圍,需要的朋友可以參考下2024-03-03
Java Volatile應用單例模式實現(xiàn)過程解析
這篇文章主要介紹了Java Volatile應用單例模式實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11
關(guān)于Java集合框架Collection接口詳解
這篇文章主要介紹了關(guān)于Java集合框架Collection接口詳解,Collection接口是Java集合框架中的基礎(chǔ)接口,定義了一些基本的集合操作,包括添加元素、刪除元素、遍歷集合等,需要的朋友可以參考下2023-05-05
Springboot中MyBatisplus使用IPage和Page分頁的實例代碼
這篇文章主要介紹了Springboot中MyBatisplus使用IPage和Page分頁,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
java使用TimeZone將中國標準時間轉(zhuǎn)成時區(qū)值
這篇文章主要介紹了java使用TimeZone將中國標準時間轉(zhuǎn)成時區(qū)值的相關(guān)資料,需要的朋友可以參考下2023-11-11
MySQL查詢字段實現(xiàn)字符串分割split功能的示例代碼
本文主要介紹了MySQL查詢字段實現(xiàn)字符串分割split功能的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
SpringBoot集成Session的實現(xiàn)示例
Session是一個在Web開發(fā)中常用的概念,它表示服務器和客戶端之間的一種狀態(tài)管理機制,用于跟蹤用戶在網(wǎng)站或應用程序中的狀態(tài)和數(shù)據(jù),本文主要介紹了SpringBoot集成Session的實現(xiàn)示例,感興趣的可以了解一下2023-09-09

