Java多線程 ThreadLocal原理解析
1、什么是ThreadLocal變量
ThreadLoal 變量,線程局部變量,同一個 ThreadLocal 所包含的對象,在不同的 Thread 中有不同的副本。
這里有幾點需要注意:
- 因為每個
Thread內(nèi)有自己的實例副本,且該副本只能由當(dāng)前Thread使用。這是也是ThreadLocal命名的由來。 - 既然每個
Thread有自己的實例副本,且其它Thread不可訪問,那就不存在多線程間共享的問題。
ThreadLocal 提供了線程本地的實例。它與普通變量的區(qū)別在于,每個使用該變量的線程都會初始化一個完全獨立的實例副本。ThreadLocal 變量通常被private static修飾。當(dāng)一個線程結(jié)束時,它所使用的所有 ThreadLocal 相對的實例副本都可被回收。
總的來說,ThreadLocal 適用于每個線程需要自己獨立的實例且該實例需要在多個方法中被使用,也即變量在線程間隔離而在方法或類間共享的場景。
2、ThreadLocal實現(xiàn)原理
首先 ThreadLocal 是一個泛型類,保證可以接受任何類型的對象。
因為一個線程內(nèi)可以存在多個 ThreadLocal 對象,所以其實是 ThreadLocal 內(nèi)部維護了一個 Map ,這個 Map 不是直接使用的 HashMap ,而是 ThreadLocal 實現(xiàn)的一個叫做 ThreadLocalMap 的靜態(tài)內(nèi)部類。而我們使用的 get() 、set() 方法其實都是調(diào)用了這個ThreadLocalMap類對應(yīng)的 get() 、set() 方法。
例如下面的 set 方法:
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
get方法:
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
createMap方法:
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
ThreadLocalMap是個靜態(tài)的內(nèi)部類:
static class ThreadLocalMap {
/**
* 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.
*/
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
/**
* The initial capacity -- MUST be a power of two.
*/
private static final int INITIAL_CAPACITY = 16;
/**
* The table, resized as necessary.
* table.length MUST always be a power of two.
*/
private Entry[] table;
/**
* The number of entries in the table.
*/
private int size = 0;
/**
* The next size value at which to resize.
*/
private int threshold; // Default to 0
/**
* Set the resize threshold to maintain at worst a 2/3 load factor.
*/
private void setThreshold(int len) {
threshold = len * 2 / 3;
}
/**
* Increment i modulo len.
*/
private static int nextIndex(int i, int len) {
return ((i + 1 < len) ? i + 1 : 0);
}
/**
* Decrement i modulo len.
*/
private static int prevIndex(int i, int len) {
return ((i - 1 >= 0) ? i - 1 : len - 1);
}
/**
* 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 = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}
...
}
最終的變量是放在了當(dāng)前線程的 ThreadLocalMap 中,并不是存在 ThreadLocal 上,ThreadLocal 可以理解為只是ThreadLocalMap的封裝,傳遞了變量值。
3、內(nèi)存泄漏問題
實際上 ThreadLocalMap 中使用的 key 為 ThreadLocal 的弱引用,弱引用的特點是,如果這個對象只存在弱引用,那么在下一次垃圾回收的時候必然會被清理掉。
所以如果 ThreadLocal 沒有被外部強引用的情況下,在垃圾回收的時候會被清理掉的,這樣一來 ThreadLocalMap中使用這個 ThreadLocal 的 key 也會被清理掉。但是,value 是強引用,不會被清理,這樣一來就會出現(xiàn) key 為 null 的 value。
ThreadLocalMap實現(xiàn)中已經(jīng)考慮了這種情況,在調(diào)用 set() 、get() 、remove() 方法的時候,會清理掉 key 為 null 的記錄。如果說會出現(xiàn)內(nèi)存泄漏,那只有在出現(xiàn)了 key 為 null 的記錄后,沒有手動調(diào)用 remove() 方法,并且之后也不再調(diào)用 get() 、set() 、remove() 方法的情況下。
4、使用場景
如上文所述,ThreadLocal 適用于如下兩種場景
每個線程需要有自己單獨的實例
實例需要在多個方法中共享,但不希望被多線程共享
對于第一點,每個線程擁有自己實例,實現(xiàn)它的方式很多。例如可以在線程內(nèi)部構(gòu)建一個單獨的實例。ThreadLoca 可以以非常方便的形式滿足該需求。
對于第二點,可以在滿足第一點(每個線程有自己的實例)的條件下,通過方法間引用傳遞的形式實現(xiàn)。ThreadLocal 使得代碼耦合度更低,且實現(xiàn)更優(yōu)雅。
1)存儲用戶Session
一個簡單的用ThreadLocal來存儲Session的例子:
private static final ThreadLocal threadSession = new ThreadLocal();
public static Session getSession() throws InfrastructureException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
s = getSessionFactory().openSession();
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
return s;
}
2)解決線程安全的問題
比如Java7中的SimpleDateFormat不是線程安全的,可以用ThreadLocal來解決這個問題:
public class DateUtil {
private static ThreadLocal<SimpleDateFormat> format1 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public static String formatDate(Date date) {
return format1.get().format(date);
}
}
這里的DateUtil.formatDate()就是線程安全的了。(Java8里的 [java.time.format.DateTimeFormatter]
(http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) 是線程安全的,Joda time里的DateTimeFormat也是線程安全的)。
public class Context {
private String name;
private String cardId;
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
public class ExecutionTask implements Runnable {
private QueryFromDBAction queryAction = new QueryFromDBAction();
private QueryFromHttpAction httpAction = new QueryFromHttpAction();
@Override
public void run() {
final Context context = new Context();
queryAction.execute(context);
System.out.println("The name query successful");
httpAction.execute(context);
System.out.println("The cardId query successful");
System.out.println("The Name is " + context.getName() + " and CardId " + context.getCardId());
}
}
public class QueryFromDBAction {
public void execute(Context context) {
try {
Thread.sleep(1000L);
String name = "Jack " + Thread.currentThread().getName();
context.setName(name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void execute(Context context) {
String name = context.getName();
String cardId = getCardId(name);
context.setCardId(cardId);
}
private String getCardId(String name) {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "444555" + Thread.currentThread().getId();
}
}
public class ContextTest {
public static void main(String[] args) {
IntStream.range(1, 5)
.forEach(i ->
new Thread(new ExecutionTask()).start()
);
}
}
The name query successful
The name query successful
The name query successful
The name query successful
The cardId query successful
The Name is Jack Thread-0 and CardId 44455511
The cardId query successful
The Name is Jack Thread-1 and CardId 44455512
The cardId query successful
The Name is Jack Thread-2 and CardId 44455513
The cardId query successful
The Name is Jack Thread-3 and CardId 44455514
問題:需要在每個調(diào)用Context的方法中傳入進去
public void execute(Context context) {
}
3)使用ThreadLocal重新設(shè)計一個上下文設(shè)計模式
public final class ActionContext {
private static final ThreadLocal<Context> threadLocal = new ThreadLocal() {
@Override
protected Object initialValue() {
return new Context();
}
};
public static ActionContext getActionContext() {
return ContextHolder.actionContext;
}
public Context getContext() {
return threadLocal.get();
}
private static class ContextHolder {
private final static ActionContext actionContext = new ActionContext();
}
}
public class Context {
private String name;
private String cardId;
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
public class ExecutionTask implements Runnable {
private QueryFromDBAction queryAction = new QueryFromDBAction();
private QueryFromHttpAction httpAction = new QueryFromHttpAction();
@Override
public void run() {
queryAction.execute();
System.out.println("The name query successful");
httpAction.execute();
System.out.println("The cardId query successful");
final Context context = ActionContext.getActionContext().getContext();
System.out.println("The Name is " + context.getName() + " and CardId " + context.getCardId());
}
}
public class QueryFromDBAction {
public void execute() {
try {
Thread.sleep(1000L);
String name = "Jack " + Thread.currentThread().getName();
ActionContext.getActionContext().getContext().setName(name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class QueryFromHttpAction {
public void execute() {
Context context = ActionContext.getActionContext().getContext();
String name = context.getName();
String cardId = getCardId(name);
context.setCardId(cardId);
}
private String getCardId(String name) {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "444555" + Thread.currentThread().getId();
}
}
public class ContextTest {
public static void main(String[] args) {
IntStream.range(1, 5)
.forEach(i ->
new Thread(new ExecutionTask()).start()
);
}
}
The name query successful
The name query successful
The name query successful
The name query successful
The cardId query successful
The Name is Jack Thread-3 and CardId 44455514
The cardId query successful
The cardId query successful
The Name is Jack Thread-0 and CardId 44455511
The cardId query successful
The Name is Jack Thread-2 and CardId 44455513
The Name is Jack Thread-1 and CardId 44455512
這樣寫 執(zhí)行過程中不會看到context的定義和聲明
注意:在使用之前記得將上個線程中context舊值清除調(diào),否則會重復(fù)調(diào)用(比如線程池操作)
4)ThreadLocal注意事項
臟數(shù)據(jù)
線程復(fù)用會產(chǎn)生臟數(shù)據(jù)。由于結(jié)程池會重用Thread對象,那么與Thread綁定的類的靜態(tài)屬性ThreadLocal變量也會被重用。如果在實現(xiàn)的線程run()方法體中不顯式地調(diào)用remove() 清理與線程相關(guān)的ThreadLocal信息,那么倘若下一個結(jié)程不調(diào)用set() 設(shè)置初始值,就可能get() 到重用的線程信息,包括 ThreadLocal所關(guān)聯(lián)的線程對象的value值。
內(nèi)存泄漏
通常我們會使用使用static關(guān)鍵字來修飾ThreadLocal(這也是在源碼注釋中所推薦的)。在此場景下,其生命周期就不會隨著線程結(jié)束而結(jié)束,寄希望于ThreadLocal對象失去引用后,觸發(fā)弱引用機制來回收Entry的Value就不現(xiàn)實了。如果不進行remove() 操作,那么這個線程執(zhí)行完成后,通過ThreadLocal對象持有的對象是不會被釋放的。
以上兩個問題的解決辦法很簡單,就是在每次用完ThreadLocal時, 必須要及時調(diào)用 remove()方法清理。
父子線程共享線程變量
很多場景下通過ThreadLocal來透傳全局上下文,會發(fā)現(xiàn)子線程的value和主線程不一致。比如用ThreadLocal來存儲監(jiān)控系統(tǒng)的某個標記位,暫且命名為traceId。某次請求下所有的traceld都是一致的,以獲得可以統(tǒng)一解析的日志文件。但在實際開發(fā)過程中,發(fā)現(xiàn)子線程里的traceld為null,跟主線程的并不一致。這就需要使用InheritableThreadLocal來解決父子線程之間共享線程變量的問題,使整個連接過程中的traceId一致。
到此這篇關(guān)于Java多線程 ThreadLocal原理解析的文章就介紹到這了,更多相關(guān)Java多線程 ThreadLocal內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中 利用正則表達式提取( )內(nèi)內(nèi)容
本篇文章,小編為大家介紹關(guān)于java中 利用正則表達式提取( )內(nèi)內(nèi)容,有需要的朋友可以參考一下2013-04-04
shiro與spring集成基礎(chǔ)Hello案例詳解
這篇文章主要介紹了shiro與spring集成基礎(chǔ)Hello案例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
Java利用Sping框架編寫RPC遠程過程調(diào)用服務(wù)的教程
這篇文章主要介紹了Java利用Sping框架編寫RPC遠程過程調(diào)用服務(wù)的教程,包括項目管理工具Maven的搭配使用方法,需要的朋友可以參考下2016-06-06
spring boot 配置freemarker及如何使用freemarker渲染頁面
springboot中自帶的頁面渲染工具為thymeleaf 還有freemarker這兩種模板引擎,本文重點給大家介紹spring boot 配置freemarker及如何使用freemarker渲染頁面,感興趣的朋友一起看看吧2023-10-10
Scala 操作Redis使用連接池工具類RedisUtil
這篇文章主要介紹了Scala 操作Redis使用連接池工具類RedisUtil,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

