java中ThreadLocal取不到值的两种原因
1.两种原因
第一种,也是最常见的一种,就是多个线程使用ThreadLocal
第二种,类加载器不同造成取不到值,本质原因就是不同类加载器造成多个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 { //默认类加载器加载StaticClassLoaderTest,并设置值 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.总结
2个累加器加载的对象引用了相同的静态变量ThreadLocal,实际上ThreadLocal并不是同一个值,所以即使在一个线程中也获取不到期望的值。
像依赖注入,如果你自己创建了一个对象,然后用手动注入了一个容器创建的依赖,假设这个依赖是自定义类加器创建的,可能会造成这种情况。
到此这篇关于java中ThreadLocal取不到值的两种原因的文章就介绍到这了,更多相关java ThreadLocal取不到值内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:
1. docker /var/lib/docker/aufs/mnt 目录清理方法2. Vuex localStorage的具体使用3. python+excel接口自动化获取token并作为请求参数进行传参操作4. Python matplotlib画图时图例说明(legend)放到图像外侧详解5. python tkinter实现下载进度条及抖音视频去水印原理6. Spring Boot如何开启并使用邮件服务7. ASP.NET泛型三之使用协变和逆变实现类型转换8. Notepad++如何安装Python插件?Notepad++插件怎么装?9. 6个常见的 PHP 安全性攻击实例和阻止方法10. vue - props 声明数组和对象操作

网公网安备