了解InheritableThreadLocal

本文深入探讨了Java中的ThreadLocal与InheritableThreadLocal的区别。通过实例代码解释了为何使用InheritableThreadLocal时,子线程可以继承父线程的初始值,而ThreadLocal则不行。文章分析了ThreadLocal的内部存储结构以及其get方法的工作原理,揭示了线程间数据隔离与继承的机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 https://www.cnblogs.com/lyp-make/p/12964517.html 观后实战:

public class ShareInThreads {

    public static void main(String[] args) {

        InheritableThreadLocal<String> threadLocal=new InheritableThreadLocal<>();
        threadLocal.set("--->哈哈哈");
        ExecutorService executorService = Executors.newFixedThreadPool(1);

        executorService.execute(()->{
            Thread thread=Thread.currentThread();
            System.out.println(thread.getName()+"__"+threadLocal.get());//输出哈哈哈
        });
        //重新修改InheritableThreadLocal内的数据
        threadLocal.set("--->呵呵呵");

        executorService.execute(()->{
            Thread thread=Thread.currentThread();
            System.out.println(thread.getName()+"__"+threadLocal.get());//输出哈哈哈
        });

        executorService.shutdown();

    }

}

 问题1:为什么两次get都是哈哈哈?

首先,得明确threadLocal的存储结构: map<ThreadLocal,Object>(为了便于理解我将ThreadLocalMap写成map),存储于Thread对象中

然后,关键的get方法:

if(map!=null){

    return map.get(threadLocal);

}else{

   //创建map,返回initialValue

}

 由于第一次get的时候,子线程没有map,则创建一个map,初始值继承父线程:“哈哈哈”;而第二次get,有map,直接返回“哈哈哈”。“呵呵呵”则是存储在父线程的map中,父子线程都的map都以同一个InheritableThreadLocal作key。

 

问题2:如果将InheritableThreadLocal替换成ThreadLocal,为什么两次get又都返回null呢?=》或者说,用ThreadLocal时,为什么子线程就不能继承父线程的初始值呢?

Thread中有两个map,一个父,一个子

/* ThreadLocal values pertaining to this thread. This map is maintained
 * by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;

/*
 * InheritableThreadLocal values pertaining to this thread. This map is
 * maintained by the InheritableThreadLocal class.
 */
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;

InheritableThreadLocal使用的是inheritableThreadLocals,并且线程初始化调用Thread.init()方法,或者查询调用在前面说的get()方法且this.inheritableThreadLocals==null时,会有以下逻辑

if (inheritThreadLocals && parent.inheritableThreadLocals != null)
    this.inheritableThreadLocals =
        ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);

 

总结:

每个线程有各自的map,使用InheritableThreadLocal时子线程在初始化时继承父线程的map(可能这是和ThreadLocal唯一的区别),之后互不影响。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值