ThreadLocal用法詳解和原理

2021-09-03 01:43:24 字數 2591 閱讀 3277

一、用法

threadlocal用於儲存某個執行緒共享變數:對於同乙個static threadlocal,不同執行緒只能從中get,set,remove自己的變數,而不會影響其他執行緒的變數。

1、threadlocal.get: 獲取threadlocal中當前執行緒共享變數的值。

2、threadlocal.set: 設定threadlocal中當前執行緒共享變數的值。

3、threadlocal.remove: 移除threadlocal中當前執行緒共享變數的值。

4、threadlocal.initialvalue: threadlocal沒有被當前執行緒賦值時或當前執行緒剛呼叫remove方法後呼叫get方法,返回此方法值。

package com.coshaho.reflect;

public class mythreadlocal

};public static void main(string args)

public static class myintegertask implements runnable

@override

public void run()

else

}try

catch (interruptedexception e)} }

}public static class mystringtask implements runnable

@override

public void run()

else

trycatch (interruptedexception e)}}

}

執行結果如下:

呼叫get方法時,當前執行緒共享變數沒有設定,呼叫initialvalue獲取預設值!

執行緒integertask1: 0

呼叫get方法時,當前執行緒共享變數沒有設定,呼叫initialvalue獲取預設值!

執行緒integertask2: 0

呼叫get方法時,當前執行緒共享變數沒有設定,呼叫initialvalue獲取預設值!

呼叫get方法時,當前執行緒共享變數沒有設定,呼叫initialvalue獲取預設值!

執行緒stringtask1: a

執行緒stringtask2: a

執行緒stringtask1: aa

執行緒stringtask2: aa

執行緒integertask1: 1

執行緒integertask2: 1

執行緒stringtask1: aaa

執行緒stringtask2: aaa

執行緒integertask2: 2

執行緒integertask1: 2

執行緒stringtask2: aaaa

執行緒stringtask1: aaaa

執行緒integertask2: 3

執行緒integertask1: 3

執行緒stringtask1: aaaaa

執行緒stringtask2: aaaaa

呼叫get方法時,當前執行緒共享變數沒有設定,呼叫initialvalue獲取預設值!

執行緒integertask2: 0

呼叫get方法時,當前執行緒共享變數沒有設定,呼叫initialvalue獲取預設值!

執行緒integertask1: 0

二、原理

執行緒共享變數快取如下:

thread.threadlocalmap;

1、thread: 當前執行緒,可以通過thread.currentthread()獲取。

2、threadlocal:我們的static threadlocal變數。

3、object: 當前執行緒共享變數。

我們呼叫threadlocal.get方法時,實際上是從當前執行緒中獲取threadlocalmap,然後根據當前threadlocal獲取當前執行緒共享變數object。threadlocal.set,threadlocal.remove實際上是同樣的道理。

這種儲存結構的好處:

1、執行緒死去的時候,執行緒共享變數threadlocalmap則銷毀。

2、threadlocalmap鍵值對數量為threadlocal的數量,一般來說threadlocal數量很少,相比在threadlocal中用map鍵值對儲存執行緒共享變數(thread數量一般來說比threadlocal數量多),效能提高很多。

關於threadlocalmap弱引用問題:

當執行緒沒有結束,但是threadlocal已經被**,則可能導致執行緒中存在threadlocalmap的鍵值對,造成記憶體洩露。(threadlocal被**,threadlocal關聯的執行緒共享變數還存在)。

雖然threadlocal的get,set方法可以清除threadlocalmap中key為null的value,但是get,set方法在記憶體洩露後並不會必然呼叫,所以為了防止此類情況的出現,我們有兩種手段。

1、使用完執行緒共享變數後,再呼叫threadlocalmap.remove方法清除執行緒共享變數;

2、jdk建議threadlocal定義為private static,這樣threadlocal的弱引用問題則不存在了。

ThreadLocal原理詳解

threadlocal稱為執行緒本地變數,其為變數在每個執行緒中都建立了乙個副本,每個執行緒都訪問和修改本執行緒中變數的副本。應用示例 可以看出,為每個執行緒分配乙個變數副本的工作並不是由threadlocal實現的,需要在應用層面實現,threadlocal只是提供乙個容器。如果在應用上為每個執行...

ThreadLocal原理詳解

首先,它是乙個資料結構,有點像hashmap,可以儲存 key value 鍵值對,但是乙個threadlocal只能儲存乙個,並且各個執行緒的資料互不干擾。1 threadlocallocalname new threadlocal 2 localname.set 尋夢 3 string name...

ThreadLocal用法簡單示例

package com.learn.test author liuqi date 2020 3 4 23 23 desc 注 threadlocal和synchronized 相比前乙個以空間取代時間,每個訪問同乙個變數的執行緒都擁有該變數的乙個副本 後乙個以時間代替空間,多個執行緒訪問會進行排隊 ...