您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)如何解析InheritableThreadLocal ,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
InheritableThreadLocal 繼承自ThreadLocal,重寫了childValue、getMap、createMap 方法,主要作用是子線程能夠讀取父線程的變量 看下這個(gè)類
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了一個(gè)新值,那結(jié)果會(huì)怎么樣? 發(fā)現(xiàn)父線程的值沒有改變
thread:son thread main:hello world
首先從新建子線程開始分析,這里主要就是將父線程的值copy到子線程中
//構(gòu)造函數(shù) 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); //新建一個(gè)Entry數(shù)組,Entry繼承了WeakReference,key為ThreadLocal類型 //這是為了在大數(shù)據(jù)量的時(shí)候,方便GC來回收已經(jīng)失效的數(shù)據(jù) 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); //計(jì)算數(shù)組索引位置,使用"線性探測(cè)法" int h = key.threadLocalHashCode & (len - 1); //如果當(dāng)前位置有值,指針需要移到下一個(gè)位置,直到找到不為null的位置 while (table[h] != null) h = nextIndex(h, len); table[h] = c; size++; } } } }
子線程獲取父線程值分析,看ThreadLocal的get方法
public T get() { Thread t = Thread.currentThread(); //實(shí)際調(diào)用InheritableThreadLocal類getMap方法,getMap返回的是當(dāng)前線程的inheritableThreadLocals變量 //每個(gè)線程都有,是Thread類的局部變量 ThreadLocalMap map = getMap(t); //如果是null會(huì)初始化一個(gè)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數(shù)組中去獲取索引對(duì)應(yīng)的值,這個(gè)table已經(jīng)在new Thread的時(shí)候copy了父線程的數(shù)據(jù) 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 //如果條件不成立,會(huì)循環(huán)整個(gè)table,并處理key失效的數(shù)據(jù) //如果遍歷完還沒找到,就返回null return getEntryAfterMiss(key, i, e); }
子線程能夠讀取父線程數(shù)據(jù),實(shí)際原因是新建子線程的時(shí)候,會(huì)從父線程copy數(shù)據(jù)
InheritableThreadLocal 繼承了ThreadLocal,并重寫childValue、getMap、createMap,對(duì)該類的操作實(shí)際上是對(duì)線程ThreadLocalMap的操作
上述就是小編為大家分享的如何解析InheritableThreadLocal 了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。