ThreadLocal提供了線程安全的數據存儲和訪問方式,利用不帶key的get和set方法,竟然能作到線程之間隔離,很是神奇。java
好比git
ThreadLocal<String> threadLocal = new ThreadLocal<>();
in thread 1github
//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("------------------------------------------"); }
輸出:app
-------------testUsage------------------ all thread done, and threadSafe is true -----------------------------------------
翻開ThreadLocal的源碼,會發現ThreadLocal只是一個空殼子,它並不存儲具體的value,而是利用當前線程(Thread.currentThread())的threadLocalMap來存儲value,key就是這個threadLocal對象自己。dom
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是咱們在外層設置的值函數
這就至關於:測試
Thread.currentThread().threadLocals.set(threadLocal1, "value1"); ..... //value的值是value1 String value = Thread.currentThread().threadLocals.get(threadLocal1);
由於每一個Thread都是不一樣的對象,因此他們的threadLocals也是不一樣的map,threadLocal在不一樣的線程裏工做時,其實是從不一樣的map裏get/set,這也就是線程安全的緣由了,瞭解到這一點就差很少了。
若是繼續翻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 ------------------------------------------
從結果上能夠看出:
查看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
從結果上能夠看出:
那麼,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....
經過查看ThreadLocal的源碼,發如今ThreadLocal對象的get/set/remove方法執行時,都有機會清除掉map中已經無用的entry。
最容易驗證清除無用entry的場景分別是:
還有其餘場景,但很差驗證,這裏就不提了。
ThreadLocal源碼就不貼了,貼了也講不明白,相關邏輯在setInitialValue、cleanSomeSlots、expungeStaleEntries、rehash、resize等方法裏。
在咱們寫代碼驗證entry回收邏輯以前,還須要簡單的提一下ThreadLocalMap的hash算法。
每一個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的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 ------------------------------------------
從結果上來看,
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前,咱們把threadLocals[一、八、六、四、2]都去掉強引用,加入新threadLocal後會發現一、八、六、四、2被清除了,但沒有擴容,由於此時size是5,小於10-10/4。這個邏輯就不貼測試結果了,你能夠取消註釋上面代碼中相關的邏輯試試。
回到現實中。
咱們用ThreadLocal的目的,無非是在跨方法調用時更方便的線程安全地存儲和使用變量。這就意味着ThreadLocal的生命週期很長,甚至和app是一塊兒存活的,強引用一直在。
既然強引用一直存在,那麼弱引用就形同虛設了。
因此在肯定再也不須要ThreadLocal中的值的狀況下,仍是老老實實的調用remove方法吧!
https://github.com/kongxiangxin/pine/tree/master/threadlocal