我知道本地線程,但直到最近才真正使用過它。
因此,我開始深入研究該主題,因為我需要一種傳播某些用戶信息的簡便方法
通過Web應用程序的不同層,而無需更改每個調用方法的簽名。
小前提信息
線程是具有自己的調用棧的單個進程。在Java中,每個調用棧有一個線程,或者每個線程有一個調用棧。即使您沒有在程序中創建任何新線程,線程也可以在沒有您的程序的情況下運行最好的例子是當您僅通過main方法啟動一個簡單的Java程序時,您沒有隱式調用new Thread()。start(),而是JVM為您創建了一個主線程來運行main方法。
主線程是非常特殊的,因為它是所有其他線程都會從中生成的線程,
線程完成后,應用程序結束了它的生命周期。
在Web應用程序服務器中,通常會有一個線程池,因為要創建的線程類非常重。所有JEE服務器(Weblogic,Glassfish,JBoss等)都有一個自調整線程池,這意味著線程池會增加或減少需要的時間,因此不會在每個請求上創建線程,而現有的線程將被重用。
了解線程局部
為了更好地理解線程本地,我將展示一種自定義線程本地的非常簡單的實現。
package ccs.progest.javacodesamples.threadlocal.ex1;import java.util.HashMap;
import java.util.Map;public class CustomThreadLocal {private static Map threadMap = new HashMap();public static void add(Object object) {threadMap.put(Thread.currentThread(), object);}public static void remove(Object object) {threadMap.remove(Thread.currentThread());}public static Object get() {return threadMap.get(Thread.currentThread());}}
因此,您可以隨時在應用程序中調用CustomThreadLocal上的add方法, 它將把當前線程作為鍵并將要與該線程關聯的對象作為值放入映射中 。 該對象可能是您希望從當前執行的線程中的任何位置訪問的對象,或者可能是您想要與該線程保持關聯并重復使用多次的昂貴對象。
您定義一個ThreadContext類,您在其中擁有要在線程內傳播的所有信息。
package ccs.progest.javacodesamples.threadlocal.ex1;public class ThreadContext {private String userId;private Long transactionId;public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public Long getTransactionId() {return transactionId;}public void setTransactionId(Long transactionId) {this.transactionId = transactionId;}public String toString() {return 'userId:' + userId + ',transactionId:' + transactionId;}}
現在是時候使用ThreadContext了。
我將啟動兩個線程,并在每個線程中添加一個新的ThreadContext實例,該實例將保存我想為每個線程傳播的信息。
package ccs.progest.javacodesamples.threadlocal.ex1;public class ThreadLocalMainSampleEx1 {public static void main(String[] args) {new Thread(new Runnable() {public void run() {ThreadContext threadContext = new ThreadContext();threadContext.setTransactionId(1l);threadContext.setUserId('User 1');CustomThreadLocal.add(threadContext);//here we call a method where the thread context is not passed as parameterPrintThreadContextValues.printThreadContextValues();}}).start();new Thread(new Runnable() {public void run() {ThreadContext threadContext = new ThreadContext();threadContext.setTransactionId(2l);threadContext.setUserId('User 2');CustomThreadLocal.add(threadContext);//here we call a method where the thread context is not passed as parameterPrintThreadContextValues.printThreadContextValues();}}).start();}
}
注意:
CustomThreadLocal.add(threadContext)是當前線程與ThreadContext實例相關聯的代碼行
您將看到執行此代碼,結果將是:
userId:User 1,transactionId:1
userId:User 2,transactionId:2
這是怎么可能的,因為我們沒有將ThreadContext,userId或trasactionId作為參數傳遞給printThreadContextValues?
package ccs.progest.javacodesamples.threadlocal.ex1;public class PrintThreadContextValues {public static void printThreadContextValues(){System.out.println(CustomThreadLocal.get());}
}
很簡單
從CustomThreadLocal的內部映射調用CustomThreadLocal.get()時,將檢索與當前線程關聯的對象。
現在,讓我們看看何時使用真正的ThreadLocal類的示例。 (上面的CustomThreadLocal類只是為了了解ThreadLocal類背后的原理,該原理非常快并以最佳方式使用內存)
package ccs.progest.javacodesamples.threadlocal.ex2;public class ThreadContext {private String userId;private Long transactionId;private static ThreadLocal threadLocal = new ThreadLocal(){@Overrideprotected ThreadContext initialValue() {return new ThreadContext();}};public static ThreadContext get() {return threadLocal.get();}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public Long getTransactionId() {return transactionId;}public void setTransactionId(Long transactionId) {this.transactionId = transactionId;}public String toString() {return 'userId:' + userId + ',transactionId:' + transactionId;}
}
如javadoc所述:ThreadLocal實例通常是希望將狀態與線程關聯的類中的私有靜態字段。
package ccs.progest.javacodesamples.threadlocal.ex2;public class ThreadLocalMainSampleEx2 {public static void main(String[] args) {new Thread(new Runnable() {public void run() {ThreadContext threadContext = ThreadContext.get();threadContext.setTransactionId(1l);threadContext.setUserId('User 1');//here we call a method where the thread context is not passed as parameterPrintThreadContextValues.printThreadContextValues();}}).start();new Thread(new Runnable() {public void run() {ThreadContext threadContext = ThreadContext.get();threadContext.setTransactionId(2l);threadContext.setUserId('User 2');//here we call a method where the thread context is not passed as parameterPrintThreadContextValues.printThreadContextValues();}}).start();}
}
調用get時 ,新的ThreadContext實例與當前線程關聯,然后將所需的值設置為ThreadContext實例。
如您所見,結果與第一組樣本相同。
userId:User 1,transactionId:1
userId:User 2,transactionId:2
(這可能是相反的順序,所以不要擔心如果您首先看到“用戶2”?)
package ccs.progest.javacodesamples.threadlocal.ex2;public class PrintThreadContextValues {public static void printThreadContextValues(){System.out.println(ThreadContext.get());}
}
ThreadLocal的另一種非常有用的用法是當您有一個非常昂貴的對象的非線程安全實例時的情況。我發現的大多數極性示例是使用SimpleDateFormat(但很快我將提供另一個使用Webservices端口的示例)
package ccs.progest.javacodesamples.threadlocal.ex4;import java.text.SimpleDateFormat;
import java.util.Date;public class ThreadLocalDateFormat {// SimpleDateFormat is not thread-safe, so each thread will have oneprivate static final ThreadLocal formatter = new ThreadLocal() {@Overrideprotected SimpleDateFormat initialValue() {return new SimpleDateFormat('MM/dd/yyyy');}};public String formatIt(Date date) {return formatter.get().format(date);}
}
結論:
線程局部變量有很多用途,這里僅描述兩種:(我認為使用最多的)
- 真正的每線程上下文,例如用戶ID或事務ID。
- 每線程實例以提高性能。
參考: Java代碼樣本博客中的JCG合作伙伴 Cristian Chiovari 了解了ThreadLocal的概念 。
翻譯自: https://www.javacodegeeks.com/2012/07/understanding-concept-behind.html