您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關如何解析InheritableThreadLocal ,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
InheritableThreadLocal 繼承自ThreadLocal,重寫了childValue、getMap、createMap 方法,主要作用是子線程能夠讀取父線程的變量 看下這個類
public class InheritableThreadLocal<T> extends ThreadLocal<T> { protected T childValue(T parentValue) { return parentValue; } //返回的是Thread類的inheritableThreadLocals,而ThreadLocal使用的是threadLocals變量 ThreadLocalMap getMap(Thread t) { return t.inheritableThreadLocals; } void createMap(Thread t, T firstValue) { t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue); } }
示例:
public class InheritableThreadLocalTest { private static final InheritableThreadLocal<String> threadLocal = new InheritableThreadLocal<String> (); public static void main(String [] args) throws InterruptedException { threadLocal.set("hello world"); Thread thread = new Thread(new Runnable() { @Override public void run() { //threadLocal.set("son thread"); System.out.println("thread:" + threadLocal.get()); } }); thread.start(); thread.join(); System.out.println("main:" + threadLocal.get()); } }
輸出:
thread:hello world main:hello world
這里如果我在子線程中set了一個新值,那結果會怎么樣? 發現父線程的值沒有改變
thread:son thread main:hello world
首先從新建子線程開始分析,這里主要就是將父線程的值copy到子線程中
//構造函數 public Thread(Runnable target) { init(null, target, "Thread-" + nextThreadNum(), 0); } //直接跳到,最終的init方法 private void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc, boolean inheritThreadLocals) { if (name == null) { throw new NullPointerException("name cannot be null"); } this.name = name; Thread parent = currentThread(); SecurityManager security = System.getSecurityManager(); //....省略中間部分,看主要的 //獲取父線程的inheritableThreadLocals變量,如果不為空就copy父線程中的變量到子線程 if (inheritThreadLocals && parent.inheritableThreadLocals != null) this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); /* Stash the specified stack size in case the VM cares */ this.stackSize = stackSize; /* Set thread ID */ tid = nextThreadID(); } //ThreadLocal.createInheritedMap方法 static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) { return new ThreadLocalMap(parentMap); } //ThreadLocalMap(parentMap) 方法 private ThreadLocalMap(ThreadLocalMap parentMap) { Entry[] parentTable = parentMap.table; int len = parentTable.length; setThreshold(len); //新建一個Entry數組,Entry繼承了WeakReference,key為ThreadLocal類型 //這是為了在大數據量的時候,方便GC來回收已經失效的數據 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) { //InheritableThreadLocal 重寫了childValue,返回value值 Object value = key.childValue(e.value); Entry c = new Entry(key, value); //計算數組索引位置,使用"線性探測法" int h = key.threadLocalHashCode & (len - 1); //如果當前位置有值,指針需要移到下一個位置,直到找到不為null的位置 while (table[h] != null) h = nextIndex(h, len); table[h] = c; size++; } } } }
子線程獲取父線程值分析,看ThreadLocal的get方法
public T get() { Thread t = Thread.currentThread(); //實際調用InheritableThreadLocal類getMap方法,getMap返回的是當前線程的inheritableThreadLocals變量 //每個線程都有,是Thread類的局部變量 ThreadLocalMap map = getMap(t); //如果是null會初始化一個value為null的ThreadLocalMap if (map != null) { //this就是InheritableThreadLocal類,看下getEntry方法 ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } return setInitialValue(); }
//這里就是從table數組中去獲取索引對應的值,這個table已經在new Thread的時候copy了父線程的數據 private Entry getEntry(ThreadLocal<?> key) { int i = key.threadLocalHashCode & (table.length - 1); Entry e = table[i]; if (e != null && e.get() == key) return e; else //如果條件不成立,會循環整個table,并處理key失效的數據 //如果遍歷完還沒找到,就返回null return getEntryAfterMiss(key, i, e); }
子線程能夠讀取父線程數據,實際原因是新建子線程的時候,會從父線程copy數據
InheritableThreadLocal 繼承了ThreadLocal,并重寫childValue、getMap、createMap,對該類的操作實際上是對線程ThreadLocalMap的操作
上述就是小編為大家分享的如何解析InheritableThreadLocal 了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。