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唯一的区别),之后互不影响。