当前位置:网站首页>异步时父子线程间的ThreadLocal传递问题
异步时父子线程间的ThreadLocal传递问题
2022-07-26 04:57:00 【知识浅谈】
异步时父子线程间的ThreadLocal传递问题
ThreadLocal的子类InheritableThreadLocal其实已经帮我们处理好了,通过这个组件可以实现父子线程之间的数据传递,在子线程中能够父线程中的ThreadLocal本地变量。
我们发现InheritableThreadLocal中createMap,以及getMap方法处理的对象不一样了,其中在ThreadLocal中处理的是threadLocals,而InheritableThreadLocal中的是inheritableThreadLocals。
createMap代码如下:
void createMap(Thread t, T firstValue) {
t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);
}
代码的意思是在Thread先获取父亲线程parent(即要创建子线程的当前这个线程)。当父亲线程中对inherThreadLocals进行了赋值,就会把当前线程的本地变量(也就是父线程的inherThreadLocals)进行createInheritedMap方法操作。查看源码createInheritedMap方法,源码可知此操作就是将赋线程的threadLocalMap传递给子线程。
边栏推荐
- STM32开发 | AD7606并行多路采集数据
- ES6模块化+CommonJS
- 创建MySQL数据库的两种方式
- SQL encryption and decryption injection details
- 擅长C(暑假每日一题 6)
- 9、 File upload and download
- idea插件离线安装(持续更新)
- Codeforces Round #807 (Div. 2)
- The first open source MySQL native HTAP database in China will be released soon! Look at the three highlights first, limited to the surrounding areas, waiting for you~
- LeetCode - 单调栈与单调队列
猜你喜欢

STM32 development | ad7606 parallel multi-channel data acquisition

Stm32fsmc extended SRAM

Vector explanation and iterator failure

data warehouse

自动化测试框架该如何搭建?

Switch and router technology: dynamic routing protocol, rip routing protocol and OSPF routing protocol

C语言——字符串函数,内存函数集锦以及模拟实现

数组排序3

UE4 controls the rotation of objects by pressing keys

There was an unexpected error (type=method not allowed, status=405)
随机推荐
11、 Exception handler
Array sort 1
数组排序1
webassembly 01基本资料
User defined type details
Face database collection summary
Several maturity levels of using MES management system
Kubernetes advanced training camp scheduler
Two ways to create MySQL database
Yapi installation
2022 Henan Mengxin League game (3): Henan University B - reverse pair count
7、 Restful
Calculate the curvature of discrete points (matlab)
2022 Henan Mengxin League game (3): Henan University L - synthetic game
Offline installation of idea plug-in (continuous update)
What points should be paid attention to in the selection of project management system?
汉字风格迁移篇---通过生成对抗网络学习一对多程式化汉字的转换和生成
Js手写函数之节流防抖函数
QT compilation error sorting and remote module Download
Niuke-top101-bm32