快速了解Java中ThreadLocal類
最近看Android FrameWork層代碼,看到了ThreadLocal這個(gè)類,有點(diǎn)兒陌生,就翻了各種相關(guān)博客一一拜讀;自己隨后又研究了一遍源碼,發(fā)現(xiàn)自己的理解較之前閱讀的博文有不同之處,所以決定自己寫篇文章說說自己的理解,希望可以起到以下作用:
- 可以疏通研究結(jié)果,加深自己的理解;
- 可以起到拋磚引玉的作用,幫助感興趣的同學(xué)疏通思路;
- 分享學(xué)習(xí)經(jīng)歷,同大家一起交流和學(xué)習(xí)。
一、 ThreadLocal 是什么
ThreadLocal 是Java類庫的基礎(chǔ)類,在包java.lang下面;
官方的解釋是這樣的:
Implements a thread-local storage, that is, a variable for which each thread has its own value. All threads share the same ThreadLocal object, but each sees a different value when accessing it, and changes made by one thread do not affect the other threads. The implementation supports null values.
大致意思是:
可以實(shí)現(xiàn)線程的本地存儲機(jī)制,ThreadLocal變量是一個(gè)不同線程可以擁有不同值的變量。所有的線程可以共享同一個(gè)ThreadLocal對象,但是不同線程訪問的時(shí)候可以取得不同的值,而且任意一個(gè)線程對它的改變不會影響其他線程。類實(shí)現(xiàn)是支持null值的(可以在set和get方法傳遞和訪問null值)。
概括來講有三個(gè)特性:
- 不同線程訪問時(shí)取得不同的值
- 任意線程對它的改變不影響其他線程
- 支持null
下面分別對這些特性進(jìn)行實(shí)例驗(yàn)證,首先定義一個(gè)Test類,在此類中我們鑒證上邊所提到的三個(gè)特性。類定義如下:
Test.java
public class Test{
//定義ThreadLocal
private static ThreadLocal name;
public static void main(String[] args) throws Exception{
name = new ThreadLocal();
//Define Thread A
Thread a = new Thread(){
public void run(){
System.out.println("Before invoke set,value is:"+name.get());
name.set(“Thread A”);
System.out.println("After invoke set, value is:"+name.get());
}
}
;
//Define Thread B
Thread b = new Thread(){
public void run(){
System.out.println("Before invoke set,value is :"+name.get());
name.set(“Thread B”);
System.out.println("After invoke set,value is :"+name.get());
}
}
;
// Not invoke set, print the value is null
System.out.println(name.get());
// Invoke set to fill a value
name.set(“Thread Main”);
// Start thread A
a.start();
a.join();
// Print the value after changed the value by thread A
System.out.println(name.get());
// Start thread B
b.start();
b.join();
// Print the value after changed the value by thread B
System.out.println(name.get())
}
}
代碼分析:
從定義中我們可以看到只聲明了一個(gè)ThreadLocal對象,其他三個(gè)線程(主線程、Thread A和Thread B)共享同一個(gè)對象;然后,在不同的線程中修改對象的值和在不同的線程中訪問對象的值,并在控制臺輸出查看結(jié)果。
看結(jié)果:

從控制臺輸出結(jié)果可以看到里邊有三個(gè)null的輸出,這個(gè)是因?yàn)樵谳敵銮皼]有對對象進(jìn)行賦值,驗(yàn)證了支持null的特點(diǎn);再者,還可以發(fā)現(xiàn)在每個(gè)線程我都對對象的值做了修改,但是在其他線程訪問對象時(shí)并不是修改后的值,而是訪問線程本地的值;這樣也驗(yàn)證了其他兩個(gè)特點(diǎn)。
二、 ThreadLocal的作用
大家都知道它的使用場景大都是多線程編程,至于具體的作用,這個(gè)怎么說那?我覺得這個(gè)只能用一個(gè)泛的說法來定義,因?yàn)橐粋€(gè)東西的功能屬性定義了以后會限制大家的思路,就好比說菜刀是用來切菜的,好多人就不會用它切西瓜了。
這里,說下我對它的作用的認(rèn)識,僅供參考,希望能有所幫助。這樣來描述吧,當(dāng)一個(gè)多線程的程序需要對多數(shù)線程的部分任務(wù)(就是run方法里的部分代碼)進(jìn)行封裝時(shí),在封裝體里就可以用ThreadLocal來包裝與線程相關(guān)的成員變量,從而保證線程訪問的獨(dú)占性,而且所有線程可以共享一個(gè)封裝體對象;可以參考下Android里的Looper。不會用代碼描述問題的程序員不是好程序員;
看代碼:統(tǒng)計(jì)線程某段代碼耗時(shí)的工具(為說明問題自造)
StatisticCostTime.java
// Class that statistic the cost time
public class StatisticCostTime{
// record the startTime
// private ThreadLocal startTime = new ThreadLocal();
private long startTime;
// private ThreadLocal costTime = new ThreadLocal();
private long costTime;
private StatisticCostTime(){
}
//Singleton
public static final StatisticCostTime shareInstance(){
return InstanceFactory.instance;
}
private static class InstanceFactory{
private static final StatisticCostTime instance = new StatisticCostTime();
}
// start
public void start(){
// startTime.set(System. nanoTime ());
startTime = System.nanoTime();
}
// end
public void end(){
// costTime.set(System. nanoTime () - startTime.get());
costTime = System.nanoTime() - startTime;
}
public long getStartTime(){
return startTime;
// return startTime.get();
}
public long getCostTime(){
// return costTime.get();
return costTime;
}
好了,工具設(shè)計(jì)完工了,現(xiàn)在我們用它來統(tǒng)計(jì)一下線程耗時(shí)試試唄:
Main.java
public class Main{
public static void main(String[] args) throws Exception{
// Define the thread a
Thread a = new Thread(){
public void run(){
try{
// start record time
StatisticCostTime.shareInstance().start();
sleep(200);
// print the start time of A
System.out.println("A-startTime:"+StatisticCostTime.shareInstance().getStartTime());
// end the record
StatisticCostTime.shareInstance().end();
// print the costTime of A
System.out.println("A:"+StatisticCostTime.shareInstance().getCostTime());
}
catch(Exception e){
}
}
}
;
// start a
a.start();
// Define thread b
Thread b = new Thread(){
public void run(){
try{
// record the start time of B1
StatisticCostTime.shareInstance().start();
sleep(100);
// print the start time to console
System.out.println("B1-startTime:"+StatisticCostTime.shareInstance().getStartTime());
// end record start time of B1
StatisticCostTime.shareInstance().end();
// print the cost time of B1
System.out.println("B1:"+StatisticCostTime.shareInstance().getCostTime());
// start record time of B2
StatisticCostTime.shareInstance().start();
sleep(100);
// print start time of B2
System.out.println("B2-startTime:"+StatisticCostTime.shareInstance().getStartTime());
// end record time of B2
StatisticCostTime.shareInstance().end();
// print cost time of B2
System.out.println("B2:"+StatisticCostTime.shareInstance().getCostTime());
}
catch(Exception e){
}
}
}
;
b.start();
}
}
運(yùn)行代碼后輸出結(jié)果是這樣的
注意:輸出結(jié)果精確度為納秒級

看結(jié)果是不是和我們預(yù)想的不一樣,發(fā)現(xiàn)A的結(jié)果應(yīng)該約等于B1+B2才對呀,怎么變成和B2一樣了那?答案就是我們在定義startTime和costTime變量時(shí),本意是不應(yīng)共享的,應(yīng)是線程獨(dú)占的才對。而這里變量隨單例共享了,所以當(dāng)計(jì)算A的值時(shí),其實(shí)startTime已經(jīng)被B2修改了,所以就輸出了和B2一樣的結(jié)果。
現(xiàn)在我們把StatisticCostTime中注釋掉的部分打開,換成ThreadLocal的聲明方式試下。
看結(jié)果:

呀!這下達(dá)到預(yù)期效果了,這時(shí)候有同學(xué)會說這不是可以線程并發(fā)訪問了嗎,是不是只要我用了ThreadLocal就可以保證線程安全了?答案是no!首先先弄明白為什么會有線程安全問題,無非兩種情況:
1、不該共享的資源,你在線程間共享了;
2、線程間共享的資源,你沒有保證有序訪問;
前者可以用“空間換時(shí)間”的方式解決,用ThreadLocal(也可以直接聲明線程局部變量),后者用“時(shí)間換空間”的方式解決,顯然這個(gè)就不是ThreadLocal力所能及的了。
三、 ThreadLocal 原理
實(shí)現(xiàn)原理其實(shí)很簡單,每次對ThreadLocal 對象的讀寫操作其實(shí)是對線程的Values對象的讀寫操作;這里澄清一下,沒有什么變量副本的創(chuàng)建,因?yàn)榫蜎]有用變量分配的內(nèi)存空間來存T對象的,而是用它所在線程的Values來存T對象的;我們在線程中每次調(diào)用ThreadLocal的set方法時(shí),實(shí)際上是將object寫入線程對應(yīng)Values對象的過程;調(diào)用ThreadLocal的get方法時(shí),實(shí)際上是從線程對應(yīng)Values對象取object的過程。
看源碼:
ThreadLocal 的成員變量set
/**
* Sets the value of this variable for the current thread. If set to
* {@code null}, the value will be set to null and the underlying entry will
* still be present.
*
* @param value the new value of the variable for the caller thread.
*/
public void set(T value) {
Thread currentThread = Thread.currentThread();
Values values = values(currentThread);
if (values == null) {
values = initializeValues(currentThread);
}
values.put(this, value);
}
TreadLocal 的成員方法get
/**
* Returns the value of this variable for the current thread. If an entry
* doesn't yet exist for this variable on this thread, this method will
* create an entry, populating the value with the result of
* {@link #initialValue()}.
*
* @return the current value of the variable for the calling thread.
*/
@SuppressWarnings("unchecked")
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);
}
ThreadLocal的成員方法initializeValues
/**
* Creates Values instance for this thread and variable type.
*/
Values initializeValues(Thread current) {
return current.localValues = new Values();
}
ThreadLocal 的成員方法values
/**
* Gets Values instance for this thread and variable type.
*/
Values values(Thread current) {
return current.localValues;
}
那這個(gè)Values又是怎樣讀寫Object那?
Values是作為ThreadLocal的內(nèi)部類存在的;這個(gè)Values里包括了一個(gè)重要數(shù)組Object[],這個(gè)數(shù)據(jù)就是解答問題的關(guān)鍵部分,它是用來存儲線程本地各種類型TreadLocal變量用的;那么問題來了,具體取某個(gè)類型的變量時(shí)是怎么保證不取到其他類型的值那?按一般的做法會用一個(gè)Map根據(jù)key-value映射一下的;對的,思路就是這個(gè)思路,但是這里并沒有用Map來實(shí)現(xiàn),是用一個(gè)Object[]實(shí)現(xiàn)的Map機(jī)制;但是,若要用Map理解的話,也是不可以的,因?yàn)闄C(jī)制是相同的;key其實(shí)上對應(yīng)ThreadLocal的弱引用,value就對應(yīng)我們傳進(jìn)去的Object。
解釋下是怎么用Object[]實(shí)現(xiàn)Map機(jī)制的(參考圖1);它是用數(shù)組下標(biāo)的奇偶來區(qū)分key和value的,就是下表是偶數(shù)的位置存儲key,奇數(shù)存儲value,就是這樣搞得;感興趣的同學(xué)如果想知道算法實(shí)現(xiàn)的話,可以深入研究一下,這里我不在詳述了。

結(jié)合前面第一個(gè)實(shí)例分析下存儲情況:
當(dāng)程序執(zhí)行時(shí)存在A,B和main三個(gè)線程,分別在線程中調(diào)用name.set()時(shí)同時(shí)針對三個(gè)線程實(shí)例在堆區(qū)分配了三塊相同的內(nèi)存空間來存儲Values對象,以name引用作為key,具體的object作為值存進(jìn)三個(gè)不同的Object[](參看下圖):

四、 總結(jié)
ThreadLocal 不能完全解決多線程編程時(shí)的并發(fā)問題,這種問題還要根據(jù)不同的情況選擇不同的解決方案,“空間換時(shí)間”還是“時(shí)間換空間”。
ThreadLocal最大的作用就是把線程共享變量轉(zhuǎn)換成線程本地變量,實(shí)現(xiàn)線程之間的隔離。
以上就是本文關(guān)于快速了解Java中ThreadLocal的全部內(nèi)容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持。
相關(guān)文章
IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
SpringBoot3整合 Elasticsearch 8.x 使用Repository構(gòu)
我們構(gòu)建了一個(gè)完整的 Spring Boot 3 和 Elasticsearch 8.x 的增刪改查示例應(yīng)用,使用 Spring Data Elasticsearch Repository,我們能夠快速實(shí)現(xiàn)對 Elasticsearch 的基本 CRUD 操作,簡化了開發(fā)流程,希望這個(gè)示例能夠幫助你理解如何在項(xiàng)目中有效使用 Elasticsearch!2024-11-11
JAVA實(shí)現(xiàn)經(jīng)典掃雷游戲的示例代碼
windows自帶的游戲《掃雷》是陪伴了無數(shù)人的經(jīng)典游戲,本程序參考《掃雷》的規(guī)則進(jìn)行了簡化,用java語言實(shí)現(xiàn),采用了swing技術(shù)進(jìn)行了界面化處理。感興趣的可以學(xué)習(xí)一下2022-01-01
Java8中新特性O(shè)ptional、接口中默認(rèn)方法和靜態(tài)方法詳解
Java 8 已經(jīng)發(fā)布很久了,很多報(bào)道表明Java 8 是一次重大的版本升級。下面這篇文章主要給大家介紹了關(guān)于Java8中新特性O(shè)ptional、接口中默認(rèn)方法和靜態(tài)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12
Java編程用棧來求解漢諾塔問題的代碼實(shí)例(非遞歸)
這篇文章主要介紹了Java編程用棧來求解漢諾塔問題的代碼實(shí)例(非遞歸),具有一定參考價(jià)值,這里給大家分享下,供朋友們參考。2017-10-10
Java Swing組件實(shí)現(xiàn)進(jìn)度監(jiān)視功能示例
這篇文章主要介紹了Java Swing組件實(shí)現(xiàn)進(jìn)度監(jiān)視功能,結(jié)合完整實(shí)例形式詳細(xì)分析了Java基于Swing組件實(shí)現(xiàn)進(jìn)度條顯示功能的具體操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-02-02

