日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

小葵花妈妈课堂开课了:《ThreadLocal 浅析》

發(fā)布時(shí)間:2023/12/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小葵花妈妈课堂开课了:《ThreadLocal 浅析》 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ThreadLocal
先看一下一下官方的解釋:

/**
* This class provides thread-local variables. These variables differ from
* their normal counterparts in that each thread that accesses one (via its
* get or set method) has its own, independently initialized
* copy of the variable. ThreadLocal instances are typically private
* static fields in classes that wish to associate state with a thread (e.g.,
* a user ID or Transaction ID).
**/

我自己翻譯了一下:
ThreadLocal類(lèi)提供了線程本地局部變量,這些局部變量區(qū)別于其他變量是因?yàn)?#xff0c;每一個(gè)線程都會(huì)有它自己的值,這個(gè)變量會(huì)在每一個(gè)線程中獨(dú)立進(jìn)行初始化。ThreadLocal實(shí)例通常都是私有靜態(tài)的,這樣做是希望將狀態(tài)與線程聯(lián)系起來(lái)。

/** Each thread holds an implicit reference to its copy of a thread-local
* variable as long as the thread is alive and the ThreadLocal
* instance is accessible; after a thread goes away, all of its copies of
* thread-local instances are subject to garbage collection (unless other
* references to these copies exist).
**/
每一個(gè)線程都隱士的引用著他自己的線程本地變量的副本,只要線程一直存活并且ThreadLocal實(shí)例可以訪問(wèn)。線程消失之后,所有的它自己的本地線程實(shí)例靠背對(duì)象都將會(huì)被回收(除非還有其他引用指向著這些副本)。

按照官方這兩段說(shuō)明,大概意思是說(shuō)ThreadLocal提供了一種能力:一個(gè)變量在多線程中都具有相同的初值,并且都存在于各自線程中彼此獨(dú)立互不影像、并且只要在同一線程中訪問(wèn)都是同一個(gè)變量。當(dāng)線程結(jié)束后,該變量也會(huì)被回收。

下面介紹一下ThreadLocal源碼:

/** * ThreadLocals rely on per-thread linear-probe hash maps attached * to each thread (Thread.threadLocals and * inheritableThreadLocals). The ThreadLocal objects act as keys, * searched via threadLocalHashCode. This is a custom hash code * (useful only within ThreadLocalMaps) that eliminates collisions * in the common case where consecutively constructed ThreadLocals * are used by the same threads, while remaining well-behaved in * less common cases. */ /** * ThreadLocal依靠線性探測(cè)哈希表附著到每個(gè)Thread. * ThreadLocal對(duì)象都使用threadLocalHashCode來(lái)進(jìn)行區(qū)分。 * 這是一個(gè)自定義的哈希值(僅用在ThreadLocalMaps內(nèi)部),用來(lái)區(qū)分在 * 同一個(gè)線程中連續(xù)構(gòu)造的多個(gè)ThreadLocal對(duì)象。 */ /** * threadLocalHashCode 在set、get、remove方法中都是將這個(gè)值當(dāng)做key進(jìn)行查找 */ private final int threadLocalHashCode = nextHashCode();public ThreadLocal(); protected T initialValue(); T get(); void set(T value); void remove();

下面介紹一下簡(jiǎn)單示例用法:

public class ThreadLocalTestActivity extends FragmentActivityRoot {private static final ThreadLocalTest threadLocalTest = new ThreadLocalTest();public static TestBean getTestBean() {TestBean testBean = threadLocalTest.get();if (testBean == null) {testBean = new TestBean();threadLocalTest.set(testBean);}return testBean;}private static class ThreadLocalTestRunable implements Runnable {@Overridepublic void run() {int count = 0;while(count < 10) {getTestBean().testInt += 100;try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.printf(Thread.currentThread().getName()+ ": threadLocal=%d\n", getTestBean().testInt);count++;}}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test_thread_local);for( int i = 0;i < 2;i++) {Thread thread = new Thread(new ThreadLocalTestRunable());thread.start();}} }

日志如下:

d: fd=88
03-26 20:52:34.354 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=101
03-26 20:52:34.354 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=101
03-26 20:52:34.455 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=201
03-26 20:52:34.455 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=201
03-26 20:52:34.556 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=301
03-26 20:52:34.556 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=301
03-26 20:52:34.656 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=401
03-26 20:52:34.656 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=401
03-26 20:52:34.757 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=501
03-26 20:52:34.757 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=501
03-26 20:52:34.857 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=601
03-26 20:52:34.857 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=601
03-26 20:52:34.957 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=701
03-26 20:52:34.957 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=701
03-26 20:52:35.059 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=801
03-26 20:52:35.060 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=801
03-26 20:52:35.162 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=901
03-26 20:52:35.165 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=901
03-26 20:52:35.264 8163-8592/com.dqs.shangri.testinstrumentation I/System.out: Thread-5: threadLocal=1001
03-26 20:52:35.266 8163-8593/com.dqs.shangri.testinstrumentation I/System.out: Thread-6: threadLocal=1001

可以看到每個(gè)線程都有自己的TestBean 對(duì)象。每個(gè)線程在調(diào)用getTestBean()時(shí)都是返回的同一個(gè)對(duì)象。
為什么會(huì)有如此特性呢,就要分析get和set源碼。

先來(lái)介紹一些T get()方法:

public T get() {Thread t = Thread.currentThread();ThreadLocalMap map = getMap(t);//第一次調(diào)用時(shí),map為nullif (map != null) {ThreadLocalMap.Entry e = map.getEntry(this);if (e != null)return (T)e.value;}//1 設(shè)置初始值return setInitialValue(); }/*** Variant of set() to establish initialValue. Used instead* of set() in case user has overridden the set() method.** @return the initial value*/ private T setInitialValue() {T value = initialValue();Thread t = Thread.currentThread();//2 getMap第一次拿到的仍為nullThreadLocalMap map = getMap(t);if (map != null)map.set(this, value);else {//3 創(chuàng)建mapcreateMap(t, value);}return value; }ThreadLocalMap getMap(Thread t) {return t.threadLocals; }//可復(fù)寫(xiě)該方法。 protected T initialValue() {return null; }ThreadLocalMap getMap(Thread t) {return t.threadLocals; }void createMap(Thread t, T firstValue) {//4 到這就明朗了,第一次使用的時(shí)候,會(huì)給每個(gè)線程的threadLocals進(jìn)行初始化,//類(lèi)型為T(mén)hreadLocal.ThreadLocalMap//一次調(diào)用的firstValue來(lái)自T initialValue();//如果沒(méi)有復(fù)寫(xiě)該方法則為nullt.threadLocals = new ThreadLocalMap(this, firstValue); }

上面的第一次get調(diào)用路徑已經(jīng)明了,
首先要拿到當(dāng)前線程Thread,如果當(dāng)前的線程的threadLocals為null,就創(chuàng)建一個(gè)。
其中創(chuàng)建的ThreadLocalMap的官方說(shuō)法為:

/**
* Construct a new map initially containing (firstKey, firstValue).
* ThreadLocalMaps are constructed lazily, so we only create
* one when we have at least one entry to put in it.
*/
構(gòu)造一個(gè)map,使用初始值(firstKey, firstValue)。ThreadLocalMaps 是懶加載的,
當(dāng)我們即將要將entry 壓入到Map中時(shí)才進(jìn)行創(chuàng)建。

也就是說(shuō)ThreadLocalMap是一種map,通過(guò)key-value形式存儲(chǔ)我們給他的值。
也就是說(shuō)每一個(gè)線程都有自己的ThreadLocalMap,key為T(mén)hreadLocal,value為任意Object。

現(xiàn)在先解釋一下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.
*/
ThreadLocalMap 是一個(gè)定制的哈希map,僅適合用來(lái)維護(hù)線程本地值。
ThreadLocal 類(lèi)外不能操作ThreadLocalMap 。
該類(lèi)僅能在Thread class中使用。
用來(lái)解決非常大和長(zhǎng)時(shí)間的引用,這些引用在哈希表內(nèi)使用弱引用方式存儲(chǔ)。
然而,自從引用不被使用時(shí),無(wú)用的條目會(huì)被刪除僅當(dāng)表的空間將被耗盡的時(shí)候。

/*** 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.*//*** 在當(dāng)前哈希Map中的條目信息是繼承自弱引用,使用他的主引用作為key(在* 當(dāng)前類(lèi)中即為T(mén)hredLocal對(duì)象)。當(dāng)keys為null的時(shí)候意味著不在被引用,* 所以這條就可以刪除。在之后的代碼中這類(lèi)條目就稱(chēng)為“stale entries”。*/ static class Entry extends WeakReference<ThreadLocal> {/** The value associated with this ThreadLocal. */Object value;Entry(ThreadLocal k, Object v) {super(k);value = v;} }//下面介紹ThreadLocalMap的成員變量 /*** The initial capacity -- MUST be a power of two.* 初始容量,一定必須是2的冪*/ private static final int INITIAL_CAPACITY = 16;/*** The table, resized as necessary.* table.length MUST always be a power of two.* table為存儲(chǔ)Entry的表,在必要的時(shí)候可以調(diào)整大小。* 表長(zhǎng)一定為2的冪*/ private Entry[] table;/*** The number of entries in the table.* table表中存儲(chǔ)元素?cái)?shù)量*/ private int size = 0;/*** The next size value at which to resize.* * 當(dāng)達(dá)到threshold值的時(shí)候,進(jìn)行resize*/ private int threshold; // Default to 0//介紹一下 構(gòu)造方法 /*** Construct a new map initially containing (firstKey, firstValue).* ThreadLocalMaps are constructed lazily, so we only create* one when we have at least one entry to put in it.*/ ThreadLocalMap(ThreadLocal firstKey, Object firstValue) {//table 為存儲(chǔ)Enrty的表結(jié)構(gòu),數(shù)組形式。容量必須為2的冪數(shù)。默認(rèn)初始值為16table = new Entry[INITIAL_CAPACITY];//每一個(gè)ThreadLocal都有一個(gè)唯一的id 即threadLocalHashCode 。//通過(guò)與15做取余操作,找到當(dāng)前ThreadLocal在table中的位置。int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);table[i] = new Entry(firstKey, firstValue);//更新table當(dāng)前容量size = 1;//設(shè)置下次resize的目標(biāo)值setThreshold(INITIAL_CAPACITY); }/*** Construct a new map including all Inheritable ThreadLocals* from given parent map. Called only by createInheritedMap.** 提供了一個(gè)可以初始table的構(gòu)造方法,僅在createInheritedMap使用。* @param parentMap the map associated with parent thread.*/ private ThreadLocalMap(ThreadLocalMap parentMap) {Entry[] parentTable = parentMap.table;int len = parentTable.length;setThreshold(len);table = new Entry[len];//方法中介紹了,當(dāng)散列表map中遇到?jīng)_突時(shí)的解決辦法,//該處使用的是開(kāi)放定址法。后文會(huì)介紹該方法。for (int j = 0; j < len; j++) {Entry e = parentTable[j];if (e != null) {ThreadLocal key = e.get();if (key != null) {Object value = key.childValue(e.value);Entry c = new Entry(key, value);int h = key.threadLocalHashCode & (len - 1);//因?yàn)槭褂瞄_(kāi)放定址法,所以h會(huì)一直向后尋找。所以table為數(shù)組形式。while (table[h] != null)h = nextIndex(h, len);table[h] = c;size++;}}} }//下面介紹ThreadLocalMap的get方法 /*** Get the entry associated with key. This method* itself handles only the fast path: a direct hit of existing* key. It otherwise relays to getEntryAfterMiss. This is* designed to maximize performance for direct hits, in part* by making this method readily inlinable.** 通過(guò)key可以獲得相關(guān)的enrty。方法本身只處理快速路徑:直接通過(guò)存在的key進(jìn)行尋找。* 如果找不到則會(huì)跳轉(zhuǎn)到getEntryAfterMiss中。這種辦法是為了能以最優(yōu)的性能找到目標(biāo),* 因?yàn)榇蟛糠质强梢酝ㄟ^(guò)改法直接找到。* * @param key the thread local object* @return the entry associated with key, or null if no such*/ private Entry getEntry(ThreadLocal key) {//首先通過(guò)key.threadLocalHashCode 和表長(zhǎng)取余,找到位置int i = key.threadLocalHashCode & (table.length - 1);Entry e = table[i];if (e != null && e.get() == key)return e;else {//如果沒(méi)有找到e,或者key不相等,則通過(guò)getEntryAfterMiss查找return getEntryAfterMiss(key, i, e);} }/*** Version of getEntry method for use when key is not found in* its direct hash slot.* 如果在直接散列表中沒(méi)有通過(guò)key找到,將調(diào)用該方法。** @param key the thread local object* @param i the table index for key's hash code* @param e the entry at table[i]* @return the entry associated with key, or null if no such*/ private Entry getEntryAfterMiss(ThreadLocal key, int i, Entry e) {Entry[] tab = table;int len = tab.length;while (e != null) {ThreadLocal k = e.get();if (k == key)return e;if (k == null) {// 如果找到的entry的key被收回,則進(jìn)行回收entry操作。expungeStaleEntry(i);} else {//因?yàn)槭褂瞄_(kāi)放地址法,直接向后尋找i = nextIndex(i, len);}e = tab[i];//直到找到空位置時(shí),return null;}return null; }/*** Expunge a stale entry by rehashing any possibly colliding entries* lying between staleSlot and the next null slot. This also expunges* any other stale entries encountered before the trailing null. See* Knuth, Section 6.4* * 在遇到?jīng)_突操作時(shí),都會(huì)進(jìn)行移除無(wú)用entry操作。移除范圍為當(dāng)前位置到下一個(gè)null直接。* * * @param staleSlot index of slot known to have null key* @return the index of the next null slot after staleSlot* (all between staleSlot and this slot will have been checked* for expunging).*/ private int expungeStaleEntry(int staleSlot) {Entry[] tab = table;int len = tab.length;// expunge entry at staleSlottab[staleSlot].value = null;tab[staleSlot] = null;size--;// Rehash until we encounter nullEntry e;int i;for (i = nextIndex(staleSlot, len);(e = tab[i]) != null;i = nextIndex(i, len)) {ThreadLocal k = e.get();if (k == null) {e.value = null;tab[i] = null;size--;} else {int h = k.threadLocalHashCode & (len - 1);if (h != i) {//當(dāng)刪除無(wú)用元素后,會(huì)對(duì)table中元素重新移動(dòng)位置。以確保下次沖突查詢(xún)時(shí),//能夠找到tab[i] = null;// Unlike Knuth 6.4 Algorithm R, we must scan until// null because multiple entries could have been stale.while (tab[h] != null)h = nextIndex(h, len);tab[h] = e;}}}return i; }//下面介紹set entry方法 /*** Set the value associated with key.** @param key the thread local object* @param value the value to be set*/ private void set(ThreadLocal key, Object value) {// We don't use a fast path as with get() because it is at// least as common to use set() to create new entries as// it is to replace existing ones, in which case, a fast// path would fail more often than not.//我們不要使用快速路徑去調(diào)用get方法,因?yàn)樗辽僭谕ǔG闆r下使用//set方法是創(chuàng)建一個(gè)新的entry用來(lái)替換先用的,在這種情況下,通常就會(huì)失敗。Entry[] tab = table;int len = tab.length;int i = key.threadLocalHashCode & (len-1);for (Entry e = tab[i];e != null;e = tab[i = nextIndex(i, len)]) {ThreadLocal k = e.get();if (k == key) {//如果當(dāng)前entry的key相同,直接替換valuee.value = value;return;}if (k == null) {//如果當(dāng)前位置的entry為staleEntry,則進(jìn)行替換replaceStaleEntry(key, value, i);return;}}//如果沒(méi)有遇到staleEntry,則進(jìn)行創(chuàng)建entrytab[i] = new Entry(key, value);int sz = ++size;//嘗試刪除無(wú)用對(duì)象,如果沒(méi)有刪除并且當(dāng)前table數(shù)量已經(jīng)大于等于threshold //將嘗試進(jìn)行resizeif (!cleanSomeSlots(i, sz) && sz >= threshold)rehash(); }/*** Heuristically scan some cells looking for stale entries.* This is invoked when either a new element is added, or* another stale one has been expunged. It performs a* logarithmic number of scans, as a balance between no* scanning (fast but retains garbage) and a number of scans* proportional to number of elements, that would find all* garbage but would cause some insertions to take O(n) time.** @param i a position known NOT to hold a stale entry. The* scan starts at the element after i.** @param n scan control: <tt>log2(n)</tt> cells are scanned,* unless a stale entry is found, in which case* <tt>log2(table.length)-1</tt> additional cells are scanned.* When called from insertions, this parameter is the number* of elements, but when from replaceStaleEntry, it is the* table length. (Note: all this could be changed to be either* more or less aggressive by weighting n instead of just* using straight log n. But this version is simple, fast, and* seems to work well.)** 當(dāng)添加新元素或者有其他的無(wú)用元素被刪除時(shí)會(huì)調(diào)用該方法啟發(fā)性的進(jìn)行刪除無(wú)用元素。* 將以對(duì)數(shù)形式進(jìn)行探索,目的為了到達(dá)一種平衡介于不進(jìn)行探索和多次掃描之間。* 這樣做可以找到所有清除對(duì)象并且只使用O(n)的時(shí)間。* * @return true if any stale entries have been removed.*/ private boolean cleanSomeSlots(int i, int n) {boolean removed = false;Entry[] tab = table;int len = tab.length;do {i = nextIndex(i, len);Entry e = tab[i];if (e != null && e.get() == null) {n = len;removed = true;//找到無(wú)用entry,即進(jìn)行刪除i = expungeStaleEntry(i);}//n無(wú)符號(hào)右移} while ( (n >>>= 1) != 0);//如果刪除了返回truereturn removed; }/** * Re-pack and/or re-size the table. First scan the entire * table removing stale entries. If this doesn't sufficiently * shrink the size of the table, double the table size. */ private void rehash() {//遍歷整個(gè)Table,刪除所有無(wú)用對(duì)象。expungeStaleEntries();// Use lower threshold for doubling to avoid hysteresis// 如果size大于等于3/4 threshold,即進(jìn)行resizeif (size >= threshold - threshold / 4)resize(); }/*** Expunge all stale entries in the table.* 刪除所有無(wú)用對(duì)象*/ private void expungeStaleEntries() {Entry[] tab = table;int len = tab.length;for (int j = 0; j < len; j++) {Entry e = tab[j];if (e != null && e.get() == null)expungeStaleEntry(j);} }/*** Double the capacity of the table.* double表的容量*/ private void resize() {Entry[] oldTab = table;int oldLen = oldTab.length;int newLen = oldLen * 2;Entry[] newTab = new Entry[newLen];int count = 0;//復(fù)制表的內(nèi)容for (int j = 0; j < oldLen; ++j) {Entry e = oldTab[j];if (e != null) {ThreadLocal k = e.get();if (k == null) {e.value = null; // Help the GC} else {int h = k.threadLocalHashCode & (newLen - 1);while (newTab[h] != null)h = nextIndex(h, newLen);newTab[h] = e;count++;}}}setThreshold(newLen);size = count;table = newTab; }/*** Replace a stale entry encountered during a set operation* with an entry for the specified key. The value passed in* the value parameter is stored in the entry, whether or not* an entry already exists for the specified key.* * 在進(jìn)行set操作時(shí),通過(guò)key找到的插入位置為無(wú)效條目時(shí),進(jìn)行替換。* 無(wú)輪key所指向的條目是否存在,都會(huì)存儲(chǔ)通過(guò)參數(shù)傳進(jìn)來(lái)的value。* * As a side effect, this method expunges all stale entries in the* "run" containing the stale entry. (A run is a sequence of entries* between two null slots.)* * 該方法有一個(gè)副作用,方法會(huì)刪除在run中的所有無(wú)效條目。* run的意思是指在兩個(gè)空槽之間的所有條目。* * @param key the key* @param value the value to be associated with key* @param staleSlot index of the first stale entry encountered while* searching for key.*/ private void replaceStaleEntry(ThreadLocal key, Object value,int staleSlot) {Entry[] tab = table;int len = tab.length;Entry e;// Back up to check for prior stale entry in current run.// We clean out whole runs at a time to avoid continual// incremental rehashing due to garbage collector freeing// up refs in bunches (i.e., whenever the collector runs).int slotToExpunge = staleSlot;//找到距離當(dāng)前插入位置最近的null條目之間的,//最遠(yuǎn)端的 無(wú)效條目slotToExpunge for (int i = prevIndex(staleSlot, len);(e = tab[i]) != null;i = prevIndex(i, len))if (e.get() == null)slotToExpunge = i;// Find either the key or trailing null slot of run, whichever// occurs firstfor (int i = nextIndex(staleSlot, len);(e = tab[i]) != null;i = nextIndex(i, len)) {ThreadLocal k = e.get();// If we find key, then we need to swap it// with the stale entry to maintain hash table order.// The newly stale slot, or any other stale slot// encountered above it, can then be sent to expungeStaleEntry// to remove or rehash all of the other entries in run.if (k == key) {e.value = value;tab[i] = tab[staleSlot];tab[staleSlot] = e;// Start expunge at preceding stale entry if it existsif (slotToExpunge == staleSlot)slotToExpunge = i;cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);return;}// If we didn't find stale entry on backward scan, the// first stale entry seen while scanning for key is the// first still present in the run.if (k == null && slotToExpunge == staleSlot)slotToExpunge = i;}// If key not found, put new entry in stale slottab[staleSlot].value = null;tab[staleSlot] = new Entry(key, value);// If there are any other stale entries in run, expunge themif (slotToExpunge != staleSlot)cleanSomeSlots(expungeStaleEntry(slotToExpunge), len); }

散列表 開(kāi)放定址法 具體介紹請(qǐng)?zhí)D(zhuǎn)此處
散列表解決沖突的辦法

那么回頭來(lái)說(shuō)get方法:

public T get() {Thread t = Thread.currentThread();//再次調(diào)用時(shí) map已經(jīng)創(chuàng)建成功ThreadLocalMap map = getMap(t);if (map != null) {//根據(jù)map的getEntry可知,如果不遇到?jīng)_突,會(huì)很快找到元素,//如果遇到?jīng)_突會(huì)使用開(kāi)放定址法來(lái)找到元素ThreadLocalMap.Entry e = map.getEntry(this);if (e != null)return (T)e.value;}return setInitialValue(); }/*** Remove the entry for key.* 刪除指定key找到的value*/ private void remove(ThreadLocal key) {Entry[] tab = table;int len = tab.length;int i = key.threadLocalHashCode & (len-1);for (Entry e = tab[i];e != null;e = tab[i = nextIndex(i, len)]) {if (e.get() == key) {e.clear();expungeStaleEntry(i);return;}} }

當(dāng)我們setEntry之后,如果遇到不再使用要主動(dòng)remove。這樣可以達(dá)到性能最優(yōu)。

總結(jié)一下:

ThreadLocal使用在多線程中,并且想要每一個(gè)線程中彼此獨(dú)立。那么使用ThreadLocal就是合適的。

ThreadLocal內(nèi)部使用數(shù)組形式的哈希表,通過(guò)開(kāi)放定址法解決沖突問(wèn)題。

ThreadLocal 通常要定義成為:

private static ThreadLocal threadLocal;

可調(diào)用方法:

protected T initialValue(); //可以重寫(xiě)該方法,賦值初始值 T get(); //使用該方法獲取值 void set(T value); //設(shè)置value void remove(); //不需要的時(shí)候remove

sy_dqs@163.com

總結(jié)

以上是生活随笔為你收集整理的小葵花妈妈课堂开课了:《ThreadLocal 浅析》的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 免费在线观看你懂的 | 欧美色图一区二区三区 | 国产性生活毛片 | 国产精品久久久久久久毛片 | 北条麻妃99精品青青久久 | 奇米四色在线视频 | 伊人春色av | 久久精品久久99 | 伊人爱爱网| 女人做爰全过程免费观看美女 | 日韩在线天堂 | 婷婷丁香六月 | 五月婷婷天堂 | 欧美日韩色图 | 日本色悠悠| 欧美乱妇狂野欧美在线视频 | 欧洲视频一区二区 | 国产又爽又黄的激情精品视频 | 日本视频免费在线播放 | 美女啪啪网站 | 一区二区三区日韩欧美 | 最新网址av | 国产精品久久久久久三级 | 日韩美女性生活 | 天天综合影院 | 亚洲韩国精品 | 国产女人18水真多18精品一级做 | 肉色丝袜小早川怜子av | 伊人激情综合 | 蜜臀视频一区二区三区 | 成人网免费看 | 超碰美女在线 | 麻豆传媒映画官网 | 免费激情视频网站 | 青青草av | 日韩电影在线观看一区二区 | 国产在线精品观看 | 久久久999精品视频 国产在线xx | av中文字幕一区 | 狠狠操狠狠操 | 国产精品久久久久久久久久辛辛 | 午夜视频91 | 午夜av中文字幕 | 国产高清第一页 | 国产精品乱码久久久久久久久 | 美女视频黄色在线观看 | 成人免费看片&#39; | 国产日韩av在线播放 | 就去干成人网 | 欧美精品一区二区三区四区 | 国产一区二区三区视频免费观看 | 亚洲福利国产 | 97日韩精品 | 噜噜噜网站 | 91精品国产亚洲 | 农民人伦一区二区三区 | 国产成人综合视频 | xxx一区二区 | 精品一区二区三区蜜臀 | 狠狠干,狠狠操 | 久久久九九九九 | 精品免费在线 | 日韩精品1| 美国色视频 | 午夜高清视频 | 美女隐私无遮挡网站 | 麻豆回家视频区一区二 | 97蜜桃网| 我把护士日出水了视频90分钟 | 成人性生活免费视频 | www.偷拍.com | 成年人在线免费 | 手机在线亚洲 | 97国产成人无码精品久久久 | 免费在线观看www | 911av | 五月婷婷深深爱 | 国内精品少妇 | 污导航在线观看 | 国产精品久久久久久白浆 | 成人av网站在线观看 | 日韩专区av| 黄色网页在线播放 | 暖暖日本在线视频 | 午夜在线一区二区 | 三上悠亚三级 | 亚洲日日日 | 亚洲视频在线观看一区二区 | 97se亚洲综合 | xxxx视频在线观看 | 久久国产福利一区 | 蜜臀久久99静品久久久久久 | 青青草国产在线视频 | 久久亚洲日本 | 久久久无码人妻精品无码 | 69pao| 亚洲最新中文字幕 | 色屁屁一区二区三区视频 | 国产高潮久久久 |