原理
產(chǎn)生線程安全問題的根源在于多線程之間的數(shù)據(jù)共享。如果沒有數(shù)據(jù)共享,就沒有多線程并發(fā)安全問題。ThreadLocal就是用來避免多線程數(shù)據(jù)共享從而避免多線程并發(fā)安全問題。它為每個(gè)線程保留一個(gè)對(duì)象的副本,避免了多線程數(shù)據(jù)共享。每個(gè)線程作用的對(duì)象都是線程私有的一個(gè)對(duì)象拷貝。一個(gè)線程的對(duì)象副本無法被其他線程訪問到(InheritableThreadLocal除外)。
注意ThreadLocal并不是一種多線程并發(fā)安全問題的解決方案,因?yàn)門hreadLocal的原理在于避免多線程數(shù)據(jù)共享從而實(shí)現(xiàn)線程安全。
來看一下JDK文檔中ThreadLocal的描述:
This class provides thread-local variables.? These variables differ fromtheir normal counterpartsinthat each thread that accesses one (via its{@code get} or {@codeset} method) has its own, independently initializedcopy of the variable.? {@code ThreadLocal} instances are typically privatestatic fieldsinclasses that wish to associate state with a thread (e.g.,a user ID or Transaction ID).Each thread holds an implicit reference to its copy of a thread-localvariable as long as the thread is alive and the {@code ThreadLocal}instance is accessible; after a thread goes away, all of its copies ofthread-local instances are subject to garbage collection (unless otherreferences to these copies exist).
其大致的意思是:ThreadLocal為每個(gè)線程保留一個(gè)對(duì)象的副本,通過set()方法和get()方法來設(shè)置和獲取對(duì)象。ThreadLocal通常被定義為私有的和靜態(tài)的,用于關(guān)聯(lián)線程的某些狀態(tài)。當(dāng)關(guān)聯(lián)ThreadLocal的線程死亡后,ThreadLocal實(shí)例才可以被GC。也就是說如果ThreadLocal關(guān)聯(lián)的線程如果沒有死亡,則ThreadLocal就一直不能被回收。
用法
創(chuàng)建
ThreadLocal<String> mThreadLocal = new ThreadLocal<>();
set方法
mThreadLocal.set(Thread.currentThread().getName());
get方法
String mThreadLocalVal = mThreadLocal.get();
設(shè)置初始值
ThreadLocal<String> mThreadLocal = ThreadLocal.withInitial(() -> Thread.currentThread().getName());
完整示例
import java.util.concurrent.atomic.AtomicInteger;public class ThreadId {? ? // Atomicintegercontaining the next thread ID to be assigned? ? private static final AtomicInteger nextId = new AtomicInteger(0);? ? // Threadlocalvariable containing each thread's ID
? ? private static final ThreadLocal<Integer> threadId =
? ? ? ? ? ? ThreadLocal.withInitial(() -> nextId.getAndIncrement());
? ? // Returns the current thread's unique ID, assigning itifnecessary? ? public static intget() {returnthreadId.get();? ? }}
源碼分析
ThreadLocal為每個(gè)線程維護(hù)一個(gè)哈希表,用于保存線程本地變量,哈希表的key是ThreadLocal實(shí)例,value就是需要保存的對(duì)象。
publlic class Thread {? ? ...? ? /* ThreadLocal values pertaining to this thread. This map is maintained? ? * by the ThreadLocal class. */? ? ThreadLocal.ThreadLocalMap threadLocals = null;? ? ...}public class ThreadLocal {? ? ...? ? static class ThreadLocalMap {? ? ? ? static class Entry extends WeakReference> {? ? ? ? ...? ? ...}
threadLocals是非靜態(tài)的,也就是說每個(gè)線程都會(huì)有一個(gè)ThreadLocalMap哈希表用來保存本地變量。ThreadLocalMap的Entry的鍵(ThreadLocal<?>)是弱引用的,也就是說當(dāng)垃圾收集器發(fā)現(xiàn)這個(gè)弱引用的鍵時(shí)不管內(nèi)存是否足夠多將其回收。這里回收的是ThreadLocalMap的Entry的ThreadLocal而不是Entry,因此還是可能會(huì)造成內(nèi)存泄露。
get()方法
public Tget() {? ? Thread t = Thread.currentThread();? ? ThreadLocalMap map = getMap(t); // 獲取線程關(guān)聯(lián)的ThreadLocalMap哈希表if(map != null) {? ? ? ? ThreadLocalMap.Entry e = map.getEntry(this); // 獲取entryif(e != null) {? ? ? ? ? ? @SuppressWarnings("unchecked")? ? ? ? ? ? T result = (T)e.value; // 返回entry關(guān)聯(lián)的對(duì)象returnresult;? ? ? ? }? ? }returnsetInitialValue(); // 如果當(dāng)前線程關(guān)聯(lián)的本地變量哈希表為空,則創(chuàng)建一個(gè)}ThreadLocalMap getMap(Thread t) {returnt.threadLocals;}
首先通過getMap()方法獲取當(dāng)前線程的ThreadLocalMap實(shí)例,ThreadLocalMap是線程私有的,因此這里是線程安全的。ThreadLocalMap的getEntry()方法用于獲取當(dāng)前線程關(guān)聯(lián)的ThreadLocalMap鍵值對(duì),如果鍵值對(duì)不為空則返回值。如果鍵值對(duì)為空,則通過setInitialValue()方法設(shè)置初始值,并返回。注意setInitialValue()方法是private,是不可以覆寫的。
設(shè)置初始值
private TsetInitialValue() {? ? T value = initialValue();? ? Thread t = Thread.currentThread();? ? ThreadLocalMap map = getMap(t);if(map != null)? ? ? ? map.set(this, value);elsecreateMap(t, value);returnvalue;}protected TinitialValue() {returnnull;}
設(shè)置初始值會(huì)調(diào)用initialValue()方法獲取初始值,該方法默認(rèn)返回null,該方法可以被覆寫,用于設(shè)置初始值。例如上面的例子中,通過匿名內(nèi)部類覆寫了initialValue()方法設(shè)置了初始值。獲取到初始值后,判斷當(dāng)前線程關(guān)聯(lián)的本地變量哈希表是否為空,如果非空則設(shè)置初始值,否則先新建本地變量哈希表再設(shè)置初始值。最后返回這個(gè)初始值。
set()方法
public voidset(T value) {? ? Thread t = Thread.currentThread();? ? ThreadLocalMap map = getMap(t);if(map != null)? ? ? ? map.set(this, value);elsecreateMap(t, value);}
該方法先獲取該線程的 ThreadLocalMap 對(duì)象,然后直接將 ThreadLocal 對(duì)象(即代碼中的 this)與目標(biāo)實(shí)例的映射添加進(jìn) ThreadLocalMap 中。當(dāng)然,如果映射已經(jīng)存在,就直接覆蓋。另外,如果獲取到的 ThreadLocalMap 為 null,則先創(chuàng)建該 ThreadLocalMap 對(duì)象。
防止內(nèi)存泄露
前面分析得知ThreadLocalMap的Entry的key是弱引用的,key可以在垃圾收集器工作的時(shí)候就被回收掉,但是存在 當(dāng)前線程->ThreadLocal->ThreadLocalMap->Entry的一條強(qiáng)引用鏈,因此如果當(dāng)前線程沒有死亡,或者還持有ThreadLocal實(shí)例的引用Entry就無法被回收。從而造成內(nèi)存泄露。
當(dāng)我們使用線程池來處理請(qǐng)求的時(shí)候,一個(gè)請(qǐng)求處理完成,線程并不一定會(huì)被回收,因此線程還會(huì)持有ThreadLocal實(shí)例的引用,即使ThreadLocal已經(jīng)沒有作用了。此時(shí)就發(fā)生了ThreadLocal的內(nèi)存泄露。
針對(duì)該問題,ThreadLocalMap 的 set 方法中,通過 replaceStaleEntry 方法將所有鍵為 null 的 Entry 的值設(shè)置為 null,從而使得該值可被回收。另外,會(huì)在 rehash 方法中通過 expungeStaleEntry 方法將鍵和值為 null 的 Entry 設(shè)置為 null 從而使得該 Entry 可被回收。通過這種方式,ThreadLocal 可防止內(nèi)存泄漏。
private voidset(ThreadLocal key, Object value) {? ? 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)]) {? ? ? ? ThreadLocal k = e.get();if(k == key) {? ? ? ? ? ? e.value = value;return;? ? ? ? }if(k == null) {? ? ? ? ? ? replaceStaleEntry(key, value, i); // key為空,則代表該Entry不再需要,設(shè)置Entry的value指針和Entry指針為null,幫助GCreturn;? ? ? ? }? ? }? ? tab[i] = new Entry(key, value);? ? int sz = ++size;if(!cleanSomeSlots(i, sz) && sz >= threshold)rehash();}
使用場(chǎng)景
ThreadLocal場(chǎng)景的使用場(chǎng)景:
每個(gè)線程需要有自己?jiǎn)为?dú)的實(shí)例
實(shí)例需要在多個(gè)方法中共享,但不希望被多線程共享
例如用來解決數(shù)據(jù)庫連接、Session管理等。
InheritableThreadLocal
ThreadLocal為每個(gè)線程保留一個(gè)線程私有的對(duì)象副本,線程之間無法共享訪問,但是有一個(gè)例外:InheritableThreadLocal,InheritableThreadLocal可以實(shí)現(xiàn)在子線程中訪問父線程中的對(duì)象副本。下面是一個(gè)InheritableThreadLocal和ThreadLocal區(qū)別的例子:
public class InheritableThreadLocalExample {? ? public static void main(String[] args) throws InterruptedException {? ? ? ? InheritableThreadLocalintegerInheritableThreadLocal = new InheritableThreadLocal<>();? ? ? ? ThreadLocalintegerThreadLocal = new ThreadLocal<>();integerInheritableThreadLocal.set(1);integerThreadLocal.set(0);? ? ? ? Thread thread = new Thread(() -> System.out.println(Thread.currentThread().getName() +", "+integerThreadLocal.get() +" / "+integerInheritableThreadLocal.get()));? ? ? ? thread.start();? ? ? ? thread.join();? ? }
運(yùn)行上述代碼會(huì)發(fā)現(xiàn)在子線程中可以獲取父線程的InheritableThreadLocal中的變量,但是無法獲取父線程的ThreadLocal中的變量。
InheritableThreadLocal是ThreadLocal的子類:
public class InheritableThreadLocal extends ThreadLocal {? ? ...? ? // 覆寫了ThreadLocal的getMap方法,返回的是Thread中的inheritableThreadLocals? ? ThreadLocalMap getMap(Thread t) {returnt.inheritableThreadLocals;? ? }? ? // 覆寫了createMap方法,創(chuàng)建的也是Thread中的inheritableThreadLocals這個(gè)哈希表? ? void createMap(Thread t, T firstValue) {? ? ? ? t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);? ? }}
而Thread的inheritableThreadLocals會(huì)在線程初始化的時(shí)候進(jìn)行初始化。這個(gè)過程在Thread類的init()方法中:
private void init(ThreadGroup g, Runnable target, String name,? ? ? ? ? ? ? ? ? long stackSize, AccessControlContext acc) {? ? ...? ? Thread parent = currentThread();? ? ...if(parent.inheritableThreadLocals != null)? ? ? ? this.inheritableThreadLocals =? ? ? ? ? ? ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);? ? ...}
一個(gè)線程在初始化的時(shí)候,會(huì)判斷創(chuàng)建這個(gè)線程的父線程的inheritableThreadLocals是否為空,如果不為空,則會(huì)拷貝父線程inheritableThreadLocals到當(dāng)前創(chuàng)建的子線程的inheritableThreadLocals中去。
當(dāng)我們?cè)谧泳€程調(diào)用get()方法時(shí),InheritableThreadLocal的getMap()方法返回的是Thread中的inheritableThreadLocals,而子線程的inheritableThreadLocals已經(jīng)拷貝了父線程的inheritableThreadLocals,因此在子線程中可以讀取父線程中的inheritableThreadLocals中保存的對(duì)象。
總結(jié)
ThreadLocal為每個(gè)線程保留對(duì)象副本,多線程之間沒有數(shù)據(jù)共享。因此它并不解決線程間共享數(shù)據(jù)的問題。
每個(gè)線程持有一個(gè)Map并維護(hù)了ThreadLocal對(duì)象與具體實(shí)例的映射,該Map由于只被持有它的線程訪問,故不存在線程安全以及鎖的問題。
ThreadLocalMap的Entry對(duì)ThreadLocal的引用為弱引用,避免了ThreadLocal對(duì)象無法被回收的問題。
ThreadLocalMap的set方法通過調(diào)用 replaceStaleEntry 方法回收鍵為 null的Entry 對(duì)象的值(即為具體實(shí)例)以及Entry對(duì)象本身從而防止內(nèi)存泄漏。
ThreadLocal 適用于變量在線程間隔離且在方法間共享的場(chǎng)景。
ThreadLocal中的變量是線程私有的,其他線程無法訪問到另外一個(gè)線程的變量。但是InheritableThreadLocal是個(gè)例外,通過InheritableThreadLocal可以在子線程中訪問到父線程中的變量。
在此我向大家推薦一個(gè)架構(gòu)學(xué)習(xí)交流群。交流學(xué)習(xí)群號(hào):938837867 暗號(hào):555 里面會(huì)分享一些資深架構(gòu)師錄制的視頻錄像:有Spring,MyBatis,Netty源碼分析,高并發(fā)、高性能、分布式、微服務(wù)架構(gòu)的原理,JVM性能優(yōu)化、分布式架構(gòu)等這些成為架構(gòu)師必備