Android 中ThreadLocal的深入理解
ThreadLocal
前言:
ThreadLocal很容易讓人望文生義,想當(dāng)然地認(rèn)為是一個“本地線程”。其實(shí),ThreadLocal并不是一個Thread,ThreadLocal是一個線程內(nèi)部的數(shù)據(jù)存儲類,通過它可以在指定的線程中存儲數(shù)據(jù),數(shù)據(jù)存儲以后,只有在指定線程中可以獲取到存儲的數(shù)據(jù),對于其它線程來說無法獲取到數(shù)據(jù)。設(shè)計(jì)初衷就是:提供線程內(nèi)部的局部變量,在本線程內(nèi)隨時可取,而隔離了其他線程。
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的時候會執(zhí)行到的方法,這里也可以看出,一個looper只能對應(yīng)一個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)建時調(diào)用了ThreadLocal類中的set方法,這里,首先獲取到當(dāng)前的線程,然后,將線程通過values的方法得到當(dāng)前線程的Values,而Values類是ThreadLocal中的一個嵌套類,用來存儲不同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對象保存在了Values類中的table成員變量中,保存的下標(biāo)是在[index+1]里,table是一個Object[]的數(shù)組。最后看看對應(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)對應(yīng)的table[]值。所以ThreadLocal我自己的理解是:不同的線程擁有不同的Values值,這個值統(tǒng)一在ThreadLocal類的table數(shù)組中,也就是說每個線程有自己的副本,在自己的副本里面讀寫信息互補(bǔ)干擾!
時間過得好快,轉(zhuǎn)眼一年了。整整快了一年沒怎么寫東西,說多了都是借口,沒有及時整理和沉淀,今年間是有點(diǎn)想法把自己平日寫的小demo總結(jié)下的,但總是忘記弄,后續(xù)得多鞭策下自己,寫點(diǎn)東西相當(dāng)于自己做個筆記,把知識框架化,不對的地方請大神們多多指教!
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android中使用CircleImageView和Cardview制作圓形頭像的方法
這篇文章主要介紹了Android中使用CircleImageView和Cardview制作圓形頭像的方法,簡單介紹了CircleImageView和Cardview的使用,需要的朋友可以參考下2016-09-09
Android Studio IDE升級4.1以后Start Failed
這篇文章主要介紹了Android Studio IDE升級4.1以后Start Failed,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
android將搜索引擎設(shè)置為中國雅虎無法搜索問題解決方法
android 進(jìn)入搜索,將搜索引擎設(shè)置為中國雅虎,無法搜索到相關(guān)網(wǎng)絡(luò)結(jié)果,該問題是由于yahoo的搜索接口改變導(dǎo)致,具體解決方法如下,感興趣的朋友可以參考下哈2013-06-06
Flutter?文字中劃線動畫StrikeThroughTextAnimation
這篇文章主要為大家介紹了Flutter?文字中劃線動畫StrikeThroughTextAnimation示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
RecyclerView中使用CheckBox出現(xiàn)勾選混亂的解決方法
這篇文章主要為大家詳細(xì)介紹了RecyclerView中使用CheckBox出現(xiàn)勾選混亂的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android編程添加快捷方式(Short)到手機(jī)桌面的方法(含添加,刪除及查詢)
這篇文章主要介紹了Android編程添加快捷方式(Short)到手機(jī)桌面的方法,含有針對桌面快捷方式的添加,刪除及查詢的操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01

