Android 中ThreadLocal的深入理解
ThreadLocal
前言:
ThreadLocal很容易讓人望文生義,想當(dāng)然地認(rèn)為是一個(gè)“本地線程”。其實(shí),ThreadLocal并不是一個(gè)Thread,ThreadLocal是一個(gè)線程內(nèi)部的數(shù)據(jù)存儲(chǔ)類,通過它可以在指定的線程中存儲(chǔ)數(shù)據(jù),數(shù)據(jù)存儲(chǔ)以后,只有在指定線程中可以獲取到存儲(chǔ)的數(shù)據(jù),對(duì)于其它線程來說無法獲取到數(shù)據(jù)。設(shè)計(jì)初衷就是:提供線程內(nèi)部的局部變量,在本線程內(nèi)隨時(shí)可取,而隔離了其他線程。
private static void prepare(boolean quitAllowed) { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); }
這段代碼就是在初始化Looper的時(shí)候會(huì)執(zhí)行到的方法,這里也可以看出,一個(gè)looper只能對(duì)應(yīng)一個(gè)thread。
public void set(T value) { Thread currentThread = Thread.currentThread(); Values values = values(currentThread); if (values == null) { values = initializeValues(currentThread); } values.put(this, value); }
looper創(chuàng)建時(shí)調(diào)用了ThreadLocal類中的set方法,這里,首先獲取到當(dāng)前的線程,然后,將線程通過values的方法得到當(dāng)前線程的Values,而Values類是ThreadLocal中的一個(gè)嵌套類,用來存儲(chǔ)不同thread的信息。
/** * Gets Values instance for this thread and variable type. */ Values values(Thread current) { return current.localValues; }
在Thread類中有這么一段:
/** * Normal thread local values. */ ThreadLocal.Values localValues;
所以從上面我們了解到set方法把當(dāng)前thread中的localValues獲取到,然后用得到的values將當(dāng)前的this和傳進(jìn)來的Looper進(jìn)行put操作:
/** * Sets entry for given ThreadLocal to given value, creating an * entry if necessary. */ void put(ThreadLocal<?> key, Object value) { cleanUp(); // Keep track of first tombstone. That's where we want to go back // and add an entry if necessary. int firstTombstone = -1; for (int index = key.hash & mask;; index = next(index)) { Object k = table[index]; if (k == key.reference) { // Replace existing entry. table[index + 1] = value; return; } if (k == null) { if (firstTombstone == -1) { // Fill in null slot. table[index] = key.reference; table[index + 1] = value; size++; return; } // Go back and replace first tombstone. table[firstTombstone] = key.reference; table[firstTombstone + 1] = value; tombstones--; size++; return; } // Remember first tombstone. if (firstTombstone == -1 && k == TOMBSTONE) { firstTombstone = index; } } }
這段代碼的意思就是將傳進(jìn)來的looper對(duì)象保存在了Values類中的table成員變量中,保存的下標(biāo)是在[index+1]里,table是一個(gè)Object[]的數(shù)組。最后看看對(duì)應(yīng)的get方法:
public T get() { // Optimized for the fast path. Thread currentThread = Thread.currentThread(); Values values = values(currentThread); if (values != null) { Object[] table = values.table; int index = hash & values.mask; if (this.reference == table[index]) { return (T) table[index + 1]; } } else { values = initializeValues(currentThread); } return (T) values.getAfterMiss(this); }
首先獲取到當(dāng)前線程,然后去取當(dāng)前線程的Values值,如果值不空,先拿table數(shù)組,再得到此values的下標(biāo),最后返回此下標(biāo)對(duì)應(yīng)的table[]值。所以ThreadLocal我自己的理解是:不同的線程擁有不同的Values值,這個(gè)值統(tǒng)一在ThreadLocal類的table數(shù)組中,也就是說每個(gè)線程有自己的副本,在自己的副本里面讀寫信息互補(bǔ)干擾!
時(shí)間過得好快,轉(zhuǎn)眼一年了。整整快了一年沒怎么寫東西,說多了都是借口,沒有及時(shí)整理和沉淀,今年間是有點(diǎn)想法把自己平日寫的小demo總結(jié)下的,但總是忘記弄,后續(xù)得多鞭策下自己,寫點(diǎn)東西相當(dāng)于自己做個(gè)筆記,把知識(shí)框架化,不對(duì)的地方請(qǐng)大神們多多指教!
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android WebView 應(yīng)用界面開發(fā)教程
WebView組件本身就是一個(gè)瀏覽器實(shí)現(xiàn),開發(fā)者可以直接在WebView中使用聚合(Polymer)和Material設(shè)計(jì)。接下來通過本文給大家介紹Android WebView 應(yīng)用界面開發(fā)教程,一起看下吧2016-08-08Android中Activity過渡動(dòng)畫的實(shí)例講解
在android5.0 以上版本中,google為我們提供了幾種activity切換的過渡動(dòng)畫,目的是為了讓 activity 切換轉(zhuǎn)場(chǎng)更加美觀,下面這篇文章主要給大家介紹了關(guān)于Android中Activity過渡動(dòng)畫的相關(guān)資料,需要的朋友可以參考下2021-11-11Android CrashHandler編寫自己的異常捕獲的方法
這篇文章主要介紹了Android CrashHandler編寫自己的異常捕獲的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Android編程實(shí)現(xiàn)TCP客戶端的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)TCP客戶端的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)TCP客戶端的原理及數(shù)據(jù)通信的相關(guān)技巧,需要的朋友可以參考下2016-04-04Android定制RadioButton樣式三種實(shí)現(xiàn)方法
三種方法實(shí)現(xiàn)Android定制RadioButton樣式:使用XML文件進(jìn)行定義/在JAVA代碼中定義等等,感興趣的朋友可以參考下,希望可以幫助到你2013-02-02Android通過ksoap2傳遞復(fù)雜數(shù)據(jù)類型及CXF發(fā)布的webservice詳細(xì)介紹
這篇文章主要介紹了 Android通過ksoap2傳遞復(fù)雜數(shù)據(jù)類型詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02用Android Studio3.0新功能加快構(gòu)建速度
本文主要介紹了使用Android Studio3.0新功能,加快Android Studio的構(gòu)建速度等相關(guān)做法。2017-11-11Kotlin ViewModelProvider.Factory的使用實(shí)例詳解
這篇文章主要介紹了Kotlin ViewModelProvider.Factory的使用,在我們使用 ViewModel 的時(shí)候,我們會(huì)發(fā)現(xiàn),有的時(shí)候我們需要用到 ViewModelFactory,有的時(shí)候不需要2023-02-02