最近作了一個日誌調用鏈路跟蹤的項目,涉及到操做標識在線程和子線程,線程池以及遠程調用之間的傳遞問題。最終採用了阿里開源的TransmittableThreadLocal
插件(https://github.com/alibaba/transmittable-thread-local)完美解決。在分析源碼以及中途修復bug的過程當中,被ThreadLocal
搞得暈頭轉向。好在靜下心來細細啃了一下午,終於能理解各類ThreadLocal
相關問題了。這裏準備用博客記錄下來。java
要想了解ThreadLocal
的底層原理首先就要了解弱引用。本篇不會詳細介紹是強引用,啥是弱引用、軟引用以及虛幻引用,有興趣的同窗能夠本身百度。這裏直接給出弱引用的簡單代碼說明:git
Object obj = new Object(); WeakReference<Object> wf = new WeakReference<Object>(obj); // 對堆內存中對象創建一個弱引用 obj = null; // 去掉堆中對象的強引用 System.gc(); System.out.println(wf.get()); // 輸出null
能夠看到弱引用的做用就在於當堆內存中對象不存在強引用的時候,在下一次gc的時候可能會回收掉堆內存佔用。github
瞭解了弱引用以後,其實就可以很好地理解ThreadLocal
(題外話:其實這個ThreadLocal我摸索了很久才弄得比較透徹
)。直接上代碼:less
首先咱們須要注意Thread
類中的兩個屬性:ide
public class Thread implements Runnable { // .... /* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ // ThreadLocal實際值的存儲所在 ThreadLocal.ThreadLocalMap threadLocals = null; /* * InheritableThreadLocal values pertaining to this thread. This map is * maintained by the InheritableThreadLocal class. */ // 後面要將的InheritableThreadLocal的實際值存儲所在 ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; }
這兩個屬性特別關鍵:ui
- 他是每一個線程所特有的
- 兩個屬性的類型是ThreadLocal的內部靜態類
他們是ThreadLocal
的神奇魔法之關鍵~this
接下來咱們來看看ThreadLocal
的關鍵方法:spa
/** * Sets the current thread's copy of this thread-local variable * to the specified value. Most subclasses will have no need to * override this method, relying solely on the {@link #initialValue} * method to set the values of thread-locals. * * @param value the value to be stored in the current thread's copy of * this thread-local. */ public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); } /** * Create the map associated with a ThreadLocal. Overridden in * InheritableThreadLocal. * * @param t the current thread * @param firstValue value for the initial entry of the map */ void createMap(Thread t, T firstValue) { // set時候的關鍵,其實是建立一個當前ThreadLocal的弱引用爲key的Map t.threadLocals = new ThreadLocalMap(this, firstValue); } /** * Returns the value in the current thread's copy of this * thread-local variable. If the variable has no value for the * current thread, it is first initialized to the value returned * by an invocation of the {@link #initialValue} method. * * @return the current thread's value of this thread-local */ public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } return setInitialValue(); } /** * Get the map associated with a ThreadLocal. Overridden in * InheritableThreadLocal. * 最爲關鍵的方法: 能夠看出getMap實際上就是獲得傳入線程的threadLocals屬性的值 * @param t the current thread * @return the map */ ThreadLocalMap getMap(Thread t) { return t.threadLocals; } /** * Removes the current thread's value for this thread-local * variable. If this thread-local variable is subsequently * {@linkplain #get read} by the current thread, its value will be * reinitialized by invoking its {@link #initialValue} method, * unless its value is {@linkplain #set set} by the current thread * in the interim. This may result in multiple invocations of the * {@code initialValue} method in the current thread. * * @since 1.5 */ public void remove() { ThreadLocalMap m = getMap(Thread.currentThread()); if (m != null) m.remove(this); }
不難看出,全部方法都是圍繞着一個ThreadLocalMap
來操做的,那麼這個ThreadLocalMap
到底是啥,咱們進一步來分析:插件
/** * ThreadLocalMap is a customized hash map suitable only for * maintaining thread local values. No operations are exported * outside of the ThreadLocal class. The class is package private to * allow declaration of fields in class Thread. To help deal with * very large and long-lived usages, the hash table entries use * WeakReferences for keys. However, since reference queues are not * used, stale entries are guaranteed to be removed only when * the table starts running out of space. */ static class ThreadLocalMap { /** * The entries in this hash map extend WeakReference, using * its main ref field as the key (which is always a * ThreadLocal object). Note that null keys (i.e. entry.get() * == null) mean that the key is no longer referenced, so the * entry can be expunged from table. Such entries are referred to * as "stale entries" in the code that follows. */ // 實際上他的存儲也是利用Entry結構來進行的,只不過這個Entry的key值是弱音用對象,實際上能夠將ThreadLocalMap看作WeakHashMap static class Entry extends WeakReference<ThreadLocal<?>> { /** The value associated with this ThreadLocal. */ Object value; Entry(ThreadLocal<?> k, Object v) { super(k); value = v; } } }
至此,咱們已經知道了ThreadLocal
是如何實現的了,具體來講是下面幾個關鍵點:線程
- ThreadLocal自己並不存儲值,而是做爲ThreadLocalMap的key用來查找對象所存儲的值的
- 用來存儲值的ThreadLocalMap是每一個線程都有的非靜態屬性,當前線程實例該屬性的值對其餘線程實例是不可見的,這也就實現了線程隔離
- ThreadLocal的get方法其實是先獲取當前線程的ThreadLocalMap屬性值,而後再經過ThreadLocal做爲key獲取實際上存儲在Map中的值
- 由於ThreadLocalMap的Key是軟引用的,因此若是ThreadLocal不存在強引用且線程被回收的話,存儲在已回收線程ThreadLocalMap中的值也是會被回收的。這一點是經過兩方面來實現的:1. Key是軟引用,當沒有強引用指向ThreadLocal時,ThreadLocalMap的以該ThreadLocal做爲key的Entry中key會在gc時被回收置爲null 2. 調用ThreadLocal的set/get/remove方法的時候會觸發Entry的expungeStaleEntry方法,方法會將key爲null的value值回收