什么时候使用 InheritableThreadLocal
一、ThreadLocal 在父子线程传递的问题
public class InheritableThreadLocalDemo {// 全局变量 // static ThreadLocal<String> threadLocal = new ThreadLocal<>();static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>();public static void main(String[] args) {new Thread(new Runnable() {@Overridepublic void run() {InheritableThreadLocalDemo.threadLocal.set("superWorld");new Service().call();}}).start();} }class Service {public void call() {System.out.println("Service:" + Thread.currentThread().getName() + " : " + InheritableThreadLocalDemo.threadLocal.get());//new Dao().call();new Thread(new Runnable() {@Overridepublic void run() {new Dao().call();}}).start();} }class Dao {public void call() {System.out.println("Dao:" + Thread.currentThread().getName() + " : " + InheritableThreadLocalDemo.threadLocal.get());}}
二、InheritableThreadLocal 和 ThreadLocal 的区别
1. InheritableThreadLocal 实现
public class InheritableThreadLocal<T> extends ThreadLocal<T> {/*** Computes the child's initial value for this inheritable thread-local* variable as a function of the parent's value at the time the child* thread is created. This method is called from within the parent* thread before the child is started.* <p>* This method merely returns its input argument, and should be overridden* if a different behavior is desired.** @param parentValue the parent thread's value* @return the child thread's initial value*/protected T childValue(T parentValue) {return parentValue;}/*** Get the map associated with a ThreadLocal.** @param t the current thread*/ThreadLocalMap getMap(Thread t) {return t.inheritableThreadLocals;}/*** Create the map associated with a ThreadLocal.** @param t the current thread* @param firstValue value for the initial entry of the table.*/void createMap(Thread t, T firstValue) {t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);} }
2. ThreadLocal 的getMap、createMap实现
/*** Get the map associated with a ThreadLocal. Overridden in* InheritableThreadLocal.** @param t the current thread* @return the map*/ThreadLocalMap getMap(Thread t) {return t.threadLocals;}/*** Create the map associated with a ThreadLocal. Overridden in* InheritableThreadLocal.** @param t the current thread* @param firstValue value for the initial entry of the map*/void createMap(Thread t, T firstValue) {t.threadLocals = new ThreadLocalMap(this, firstValue);}
通过上面的代码我们可以看到InheritableThreadLocal 重写了childValue, getMap,createMap三个方法,
当我们往里面set值的时候,值保存到了inheritableThreadLocals里面,而不是之前的threadLocals。
三、问题:为什么当创建子线程时,可以获取到上个线程里的threadLocal中的值呢?
原因就是在新创建线程的时候,会把之前线程的inheritableThreadLocals赋值给新线程的inheritableThreadLocals,通过这种方式实现了数据的传递。
代码关键点:
1. Thread#init
if (parent.inheritableThreadLocals != null)this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
2. ThreadLocal.createInheritedMap
static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {return new ThreadLocalMap(parentMap);}private ThreadLocalMap(ThreadLocalMap parentMap) {Entry[] parentTable = parentMap.table;int len = parentTable.length;setThreshold(len);table = new Entry[len];for (int j = 0; j < len; j++) {Entry e = parentTable[j];if (e != null) {@SuppressWarnings("unchecked")ThreadLocal<Object> key = (ThreadLocal<Object>) e.get();if (key != null) {Object value = key.childValue(e.value);Entry c = new Entry(key, value);int h = key.threadLocalHashCode & (len - 1);while (table[h] != null)h = nextIndex(h, len);table[h] = c;size++;}}}}
什么时候使用 InheritableThreadLocal相关推荐
- 每日一博 - ThreadLocal VS InheritableThreadLocal VS TransmittableThreadLocal
文章目录 ThreadLocal 核心API ThreadLocal类 源码分析 set get remove 缺陷 InheritableThreadLocal 源码解析 局限性 Transmitt ...
- Java 多线程:InheritableThreadLocal 实现原理
前言 介绍 InheritableThreadLocal 之前,假设对 ThreadLocal 已经有了一定的理解,比如基本概念,原理,如果没有,可以参考:Java 多线程:threadlocal关键 ...
- java线程之InheritableThreadLocal
ThreadLocal和InheritableThreadLocal区别,可以用代码测试一下: [java] view plain copy package com.lang; import juni ...
- ThreadLocal 和 InheritableThreadLocal
在学习ThreadLocal之前,建议先了解Java中的4种引用 一.先看一下Thread,ThreadMap,ThreadLocal的关系 Thread中持有一个ThreadLocalMap ,这里 ...
- ThreadLoacl,InheritableThreadLocal,原理,以及配合线程池使用的一些坑
虽然使用AOP可以获取方法签名,但是如果要获取方法中计算得出的数据,那么就得使用ThreadLocal,如果还涉及父线程,那么可以选择InheritableThreadLocal. 注意:理解一些原理 ...
- TransmittableThreadLocal 解决 线程池线程复用 无法复制 InheritableThreadLocal 的问题
ThreadLoacl,InheritableThreadLocal,原理,以及配合线程池使用的一些坑 TransmittableThreadLocal 原理 之前为了能让InheritableThr ...
- TransmittableThreadLocal 解决 线程池线程复用 无法复制 InheritableThreadLocal 的问题.
ThreadLoacl,InheritableThreadLocal,原理,以及配合线程池使用的一些坑 TransmittableThreadLocal 原理 之前为了能让InheritableThr ...
- ThreadLocal系列(二)-InheritableThreadLocal的使用及原理解析
ThreadLocal系列之InheritableThreadLocal的使用及原理解析(源码基于java8) 上一篇:ThreadLocal系列(一)-ThreadLocal的使用及原理解析 下一篇 ...
- ThreadLocal和InheritableThreadLocal使用
InheritableThreadLocal代码 public class InheritableThreadLocal<T> extends ThreadLocal<T> { ...
最新文章
- 麻省理工和 IBM 合作创立 Watson AI 实验室,谨慎推进 AI 研究
- IT风险管理专家CISRE认证
- NET问答:如何理解 IEnumerableT 和 IQueryableT
- 操作目录下的文件或目录
- 进击的二维码 | ArcBlock 课堂预告
- 计算机内部程序代码,计算机为什么能够读懂程序代码?
- 35 MM配置-采购-采购订单-设置价格差异的容差限制
- 布局中文件中【控件间距参数详解以及单位选择】
- 例解List<Map<String, Object>>存放的对象问题
- 用.net改写的uploadify多文件上传控件
- C#开根号函数:math.pow()函数
- Powerdesigner下载安装
- 福禄克FLUKE OptiFiber Pro HDR OTDR光时域反射(OTDR)测试仪OFP2-100-S
- win7计算机管理打开超慢,大师解答win7系统打开资源管理器窗口速度很慢的恢复技巧...
- Baklib每日分享|在线产品手册的制作技巧
- php euc-cn,php配置中文解说
- SE93 创建参数事务
- STM32(CM3内核) 内存映射
- PBC【Packaged Business Capabilities】的理解
- (转)什么是PR?什么是BD?
热门文章
- LVS负载均衡群集(LVS-NAT搭建)
- 互换性与技术测量电子版_181套建设工程全套资料表格,从开工到完工,完整电子版手慢无...
- arm cortex-a8 天梯图_ARM正式推出CortexA78C核心:针对笔记本电脑设计、支持8个大核心...
- java程序中可以如何异常处理?_如何处理罗茨鼓风机在运行过程中出现异常噪音和叶片的运行特性...
- html页面设置不可点击事件,css怎么设置div不可点击?
- 网络电缆 计算机电缆,计算机电缆的技术参数
- oracle where in优化,【求助】为什么在where过滤条件中使用in这样子查询后速度变的非常慢?!...
- 漏磁用MATLAB,管道漏磁内检测数据可视化技术研究
- c语言排序算法 应用与实现,基于C语言排序算法改进与应用.doc
- java map映射_【Java】Map 映射接口 概述