java中ThreadLocal取不到值的兩種原因
1.兩種原因
第一種,也是最常見的一種,就是多個線程使用ThreadLocal
第二種,類加載器不同造成取不到值,本質(zhì)原因就是不同類加載器造成多個ThreadLocal對象
public class StaticClassLoaderTest {
protected static final ThreadLocal<Object> local = new ThreadLocal<Object>();
//cusLoader加載器加載的對象
private Test3 test3;
public StaticClassLoaderTest() {
try {
test3 = (Test3) Class.forName("gittest.Test3", true, new cusLoader()).newInstance();
}
catch (Exception e) {
e.printStackTrace();
}
}
public Test3 getTest3() {
return test3;
}
public static void main(String[] args) {
try {
//默認(rèn)類加載器加載StaticClassLoaderTest,并設(shè)置值
StaticClassLoaderTest.local.set(new Object());
new StaticClassLoaderTest().getTest3();
}
catch (Exception e) {
e.printStackTrace();
}
}
//自定義類加載器
public static class cusLoader extends ClassLoader {
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.contains("StaticClassLoaderTest")) {
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(name.replace(".", "/") + ".class");
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
IOUtils.copy(is, output);
return defineClass(output.toByteArray(), 0, output.toByteArray().length);
}
catch (IOException e) {
e.printStackTrace();
}
}
return super.loadClass(name, resolve);
}
}
}
public class Test3 {
public void test() {
//由cusLoader加載器加載StaticClassLoaderTest,并獲取值,由于StaticClassLoaderTest并不相同所以無法獲取到值
System.out.println(StaticClassLoaderTest.local.get());
}
}
2.總結(jié)
2個累加器加載的對象引用了相同的靜態(tài)變量ThreadLocal,實(shí)際上ThreadLocal并不是同一個值,所以即使在一個線程中也獲取不到期望的值。
像依賴注入,如果你自己創(chuàng)建了一個對象,然后用手動注入了一個容器創(chuàng)建的依賴,假設(shè)這個依賴是自定義類加器創(chuàng)建的,可能會造成這種情況。
到此這篇關(guān)于java中ThreadLocal取不到值的兩種原因的文章就介紹到這了,更多相關(guān)java ThreadLocal取不到值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java中實(shí)現(xiàn)SHA1與MD5加密算法的基本方法
這篇文章主要介紹了詳解Java中實(shí)現(xiàn)SHA1與MD5加密算法的基本方法,安全哈希算法第一版和消息摘要算法第五版也是通常人們最常用的加密算法,需要的朋友可以參考下2016-04-04
Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn)
這篇文章主要介紹了Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn),文中通過示例代碼以及圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot+MyBatis-Plus+Velocity實(shí)現(xiàn)代碼自動生成
本文主要介紹了使用SpringBoot、MyBatis-Plus和Velocity模板引擎實(shí)現(xiàn)代碼自動生成器,該生成器能夠根據(jù)數(shù)據(jù)庫表結(jié)構(gòu)自動生成增刪改查操作的代碼,感興趣的可以了解一下2025-03-03
SpringBoot3集成SpringSecurity+JWT的實(shí)現(xiàn)
本文詳解SpringBoot3整合SpringSecurity與JWT實(shí)現(xiàn)認(rèn)證授權(quán),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-07-07
Java CompletableFuture如何實(shí)現(xiàn)超時功能
這篇文章主要為大家介紹了實(shí)現(xiàn)超時功能的基本思路以及CompletableFuture(之后簡稱CF)是如何通過代碼實(shí)現(xiàn)超時功能的,需要的小伙伴可以了解下2025-01-01

