在多線程的環境中,咱們常常使用鎖來保證線程的安全,可是對於每一個線程都要用的資源使用鎖的話那麼程序執行的效率就會受到影響,這個時候能夠把這些資源變成線程封閉的形式。html
所謂的棧封閉其實就是使用局部變量存放資源,咱們知道局部變量在內存中是存放在虛擬機棧中,而棧又是每一個線程私有獨立的,因此這樣能夠保證線程的安全。小程序
咱們先看ThreadLocal和線程Thread的關係圖。安全
再看下ThreadLocal的操做,以get爲例多線程
public T get() { // 當前線程 Thread t = Thread.currentThread(); // 拿到當前線程的threadLocalMap,即上圖中的map引用 ThreadLocalMap map = getMap(t); if (map != null) { // 拿到當前ThreadLocal爲Key對應的Entry,裏面作了防止內存泄漏的處理 ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } // 若是爲null設置默認值 return setInitialValue(); }
如上面get方法的源碼所示,在調用threadLocal.get()方法的時候,threadLocal拿到當前線程中ThreadLocalMap中以threadLocal自身爲key對應的entry,在這個getEntry方法中裏面作了內存泄漏的處理,大概處理邏輯就是若是threadLocal對應的Entry爲null的話,讓這個entry的value爲null而且map中threadLocal對應下標置null,若是不爲null的話返回,不然的話則調用默認值方法setInitialValue()ide
private T setInitialValue() { T value = initialValue(); Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); return value; } // 默認null實現 protected T initialValue() { return null; }
setInitialValue()方法邏輯比較簡單,這裏很少贅述,值得注意的是裏面調用的initialValue(),並無任何的實現,因此咱們使用threadLocal的時候通常都會選擇重寫實現這個方法。測試
// 這裏main方法測試,因此用static修飾,會延長threadLocal的生命週期,有內存泄漏的風險,通常做爲成員變量就足夠了 public static ThreadLocal<String> threadLocal = new ThreadLocal<String>(){ @Override protected String initialValue() { return "init string from initialValue method"; } }; public static void main(String[] args) throws InterruptedException { // 未放入value直接調用get System.err.println("invoke get before any set:" + threadLocal.get()); threadLocal.set("test"); System.err.println("before thread start : " + threadLocal.get()); new Thread(() -> { // 對相同的threadLocal對象放入值 threadLocal.set("test in thread"); System.err.println("In thread[" + Thread.currentThread().getName() + "] threadLocal value : " + threadLocal.get()); }).start(); TimeUnit.SECONDS.sleep(1); // 證實threadLocal中的value不在線程中共享 System.err.println("after thread value : " + threadLocal.get()); }result:
結合這個小程序和上面的圖就能夠對threadLocal有一個大概的理解了。其餘的方法如set、remove等方法都大同小異,能夠結合圖片去看源碼,這裏再也不贅述。
一、在threadLocal的get、set、remove方法中,其對自己可能發生的內存泄漏都作了處理,邏輯上面也提到若是對應entry爲null,將其value置null,將map中對應下標引用置null。this
二、而對於threadLocal中這個對象的泄漏來講,則是採用弱引用的方式來實現,在上面的圖中,我用虛線來表示弱引用,弱引用的意思是在JVM進行垃圾回收的時候這個引用會被回收(不管內存足夠與否);試想一下,若是使用強引用而且棧中的引用消失了,那麼在線程結束以前這個threadLocal對象不會被回收且沒法訪問,也就是形成內存泄漏。spa
上面在ThreadLocal提到了弱引用,這裏順便簡單的說下Java中的四種引用。線程
如有不正之處,望指出!