[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>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并不是同一个值,所以即使在一个线程中也获取不到期望的值。
* 像依赖注入,如果你自己创建了一个对象,然后用手动注入了一个容器创建的依赖,假设这个依赖是自定义类加器创建的,可能会造成这种情况。