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并不是同一个值,所以即使在一个线程中也获取不到期望的值。
  • 像依赖注入,如果你自己创建了一个对象,然后用手动注入了一个容器创建的依赖,假设这个依赖是自定义类加器创建的,可能会造成这种情况。
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值