如何解析InheritableThreadLocal
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,这期内容当中小编将会给大家带来有关如何解析InheritableThreadLocal ,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。InheritableThre
千家信息网最后更新 2025年02月23日如何解析InheritableThreadLocal
这期内容当中小编将会给大家带来有关如何解析InheritableThreadLocal ,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
InheritableThreadLocal 继承自ThreadLocal,重写了childValue、getMap、createMap 方法,主要作用是子线程能够读取父线程的变量 看下这个类
public class InheritableThreadLocalextends ThreadLocal { protected T childValue(T parentValue) { return parentValue; } //返回的是Thread类的inheritableThreadLocals,而ThreadLocal使用的是threadLocals变量 ThreadLocalMap getMap(Thread t) { return t.inheritableThreadLocals; } void createMap(Thread t, T firstValue) { t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue); }}
示例:
public class InheritableThreadLocalTest { private static final InheritableThreadLocalthreadLocal = new InheritableThreadLocal (); public static void main(String [] args) throws InterruptedException { threadLocal.set("hello world"); Thread thread = new Thread(new Runnable() { @Override public void run() { //threadLocal.set("son thread"); System.out.println("thread:" + threadLocal.get()); } }); thread.start(); thread.join(); System.out.println("main:" + threadLocal.get()); }}
输出:
thread:hello worldmain:hello world
这里如果我在子线程中set了一个新值,那结果会怎么样? 发现父线程的值没有改变
thread:son threadmain:hello world
源码剖析
首先从新建子线程开始分析,这里主要就是将父线程的值copy到子线程中
//构造函数public Thread(Runnable target) { init(null, target, "Thread-" + nextThreadNum(), 0);}//直接跳到,最终的init方法private void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc, boolean inheritThreadLocals) { if (name == null) { throw new NullPointerException("name cannot be null"); } this.name = name; Thread parent = currentThread(); SecurityManager security = System.getSecurityManager(); //....省略中间部分,看主要的 //获取父线程的inheritableThreadLocals变量,如果不为空就copy父线程中的变量到子线程 if (inheritThreadLocals && parent.inheritableThreadLocals != null) this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); /* Stash the specified stack size in case the VM cares */ this.stackSize = stackSize; /* Set thread ID */ tid = nextThreadID();}//ThreadLocal.createInheritedMap方法static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) { return new ThreadLocalMap(parentMap);} //ThreadLocalMap(parentMap) 方法private ThreadLocalMap(ThreadLocalMap parentMap) { Entry[] parentTable = parentMap.table; int len = parentTable.length; setThreshold(len); //新建一个Entry数组,Entry继承了WeakReference,key为ThreadLocal类型 //这是为了在大数据量的时候,方便GC来回收已经失效的数据 table = new Entry[len]; for (int j = 0; j < len; j++) { Entry e = parentTable[j]; if (e != null) { @SuppressWarnings("unchecked") ThreadLocal
子线程获取父线程值分析,看ThreadLocal的get方法
public T get() { Thread t = Thread.currentThread(); //实际调用InheritableThreadLocal类getMap方法,getMap返回的是当前线程的inheritableThreadLocals变量 //每个线程都有,是Thread类的局部变量 ThreadLocalMap map = getMap(t); //如果是null会初始化一个value为null的ThreadLocalMap if (map != null) { //this就是InheritableThreadLocal类,看下getEntry方法 ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } return setInitialValue();}
//这里就是从table数组中去获取索引对应的值,这个table已经在new Thread的时候copy了父线程的数据private Entry getEntry(ThreadLocal> key) { int i = key.threadLocalHashCode & (table.length - 1); Entry e = table[i]; if (e != null && e.get() == key) return e; else //如果条件不成立,会循环整个table,并处理key失效的数据 //如果遍历完还没找到,就返回null return getEntryAfterMiss(key, i, e);}
总结
子线程能够读取父线程数据,实际原因是新建子线程的时候,会从父线程copy数据
InheritableThreadLocal 继承了ThreadLocal,并重写childValue、getMap、createMap,对该类的操作实际上是对线程ThreadLocalMap的操作
上述就是小编为大家分享的如何解析InheritableThreadLocal 了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。
线程
方法
变量
数据
位置
分析
实际
就是
数组
时候
内容
索引
专业
中小
作用
内容丰富
函数
原因
实际上
局部
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全周授课
网络安全 技术发展方向
计算机网络技术要学习哪几种语言
软件开发驻场服务费
数据库引擎干嘛的
软件开发简历实践经历
免费云服务器永久使用推荐
关于网络安全的新闻简介
新罗区骁嘉波网络技术服务部
安装数据库5
全国统一数据库查询
网络安全审查 李青
广州软件开发什么公司待遇好
历史上有多少数据库
服务器机房可以办公吗
无线传感器网络安全插画
ftp服务器程序源代码下载
佛山服务器分销商
软件开发计划图书管理系统
中新赛克软件开发笔试
神武服务器65级什么时候开始
专业软件开发创意
西班牙中国网络安全
人大金仓数据库属于哪种类型
保定大为软件开发公司面试
所有软件开发都要测试么
梦幻新买的号转回原服务器
网络安全等级保护方案
资阳软件开发费用是多少
服务器安全狗攻击日志