java開發(fā)工作中對(duì)InheritableThreadLocal使用思考
引言
最近在工作中結(jié)合線程池使用 InheritableThreadLocal 出現(xiàn)了獲取線程變量“錯(cuò)誤”的問題,看了相關(guān)的文檔和源碼后在此記錄。
1. 先說結(jié)論
InheritableThreadLocal 只有在父線程創(chuàng)建子線程時(shí),在子線程中才能獲取到父線程中的線程變量;
當(dāng)配合線程池使用時(shí):“第一次在線程池中開啟線程,能在子線程中獲取到父線程的線程變量,而當(dāng)該子線程開啟之后,發(fā)生線程復(fù)用,該子線程仍然保留的是之前開啟它的父線程的線程變量,而無法獲取當(dāng)前父線程中新的線程變量”,所以會(huì)發(fā)生獲取線程變量錯(cuò)誤的情況。
2. 實(shí)驗(yàn)例子
- 創(chuàng)建一個(gè)線程數(shù)固定為1的線程池,先在main線程中存入變量1,并使用線程池開啟新的線程打印輸出線程變量,之后更改main線程的線程變量為變量2,再使用線程池中線程(發(fā)生線程復(fù)用)打印輸出線程變量,對(duì)比兩次輸出的值是否不同
/**
* 測(cè)試線程池下InheritableThreadLocal線程變量失效的場(chǎng)景
*/
public class TestInheritableThreadLocal {
private static final InheritableThreadLocal<String> threadLocal = new InheritableThreadLocal<>();
// 固定大小的線程池,保證線程復(fù)用
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");
// 線程復(fù)用再取還是 main線程 變量1
executorService.execute(() -> System.out.println(threadLocal.get()));
}
}
輸出結(jié)果:
main線程 變量1 main線程 變量1
發(fā)現(xiàn)兩次輸出結(jié)果值相同,證明發(fā)生線程復(fù)用時(shí),子線程獲取父線程變量失效
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, 以能夠讓子線程能夠從父線程中繼承線程變量: 當(dāng)一個(gè)子線程被創(chuàng)建時(shí),它會(huì)接收到父線程中所有可繼承的變量。通常情況下,子線程和父線程中的線程變量是完全相同的,但是可以通過重寫 childValue 方法來使父子線程中的值不同。
當(dāng)線程中維護(hù)的變量如UserId, TransactionId 等必須自動(dòng)傳遞到新創(chuàng)建的任何子線程時(shí),使用InheritableThreadLocal要優(yōu)于ThreadLocal
3.2 源碼
public class InheritableThreadLocal<T> extends ThreadLocal<T> {
/**
* 當(dāng)子線程被創(chuàng)建時(shí),通過該方法來初始化子線程中線程變量的值,
* 這個(gè)方法在父線程中被調(diào)用,并且在子線程開啟之前。
*
* 通過重寫這個(gè)方法可以改變從父線程中繼承過來的值。
*
* @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方法來獲取父線程中的線程變量的值,也可通過重寫這個(gè)方法來將獲取到的線程變量的值進(jìn)行修改。
在getMap方法和createMap方法中,可以發(fā)現(xiàn)inheritableThreadLocals變量,它是 ThreadLocalMap,在Thread類中

3.2.1 childValue方法
- 開啟新線程時(shí),會(huì)調(diào)用Thread的構(gòu)造方法
public Thread(ThreadGroup group, String name) {
init(group, null, name, 0);
}
- 沿著構(gòu)造方法向下,找到
init方法的最終實(shí)現(xiàn),其中有如下邏輯:為當(dāng)前線程創(chuàng)建線程變量以繼承父線程中的線程變量
/**
* @param inheritThreadLocals 為ture,代表是為 包含可繼承的線程變量 的線程進(jìn)行初始化
*/
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 的具體實(shí)現(xiàn)
createInheritedMap 方法,最終會(huì)調(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++;
}
}
}
}
這個(gè)方法會(huì)對(duì)父線程中的線程變量做深拷貝,其中調(diào)用了childValue方法來獲取/初始化子線程中的值,并保存到子線程中
- 由上可見,可繼承的線程變量只是在線程被創(chuàng)建的時(shí)候進(jìn)行了初始化工作,這也就能解釋為什么在線程池中發(fā)生線程復(fù)用時(shí)不能獲取到父線程線程變量的原因
4. 實(shí)驗(yàn)例子流程圖

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

