当前位置:网站首页>JD side: how can a child thread get the value of the parent thread ThreadLocal? I got...
JD side: how can a child thread get the value of the parent thread ThreadLocal? I got...
2022-04-23 01:03:00 【Java technology stack】
source :blog.csdn.net/weixin_44912855
Recently, a netizen in the wechat group shared the process of his Jingdong side , Let me share with you one of the interview questions .
On the east side of Beijing 」 How does a child thread get the parent thread ThreadLocal Value
How does a child thread get the parent thread ThreadLocal Value

Want the child thread to get the parent thread ThreadLocal The value in , Its subclasses are required InheritableThreadLocal Realization .
The test code is as follows :
public static void main(String[] args) throws InterruptedException { Thread parentParent = new Thread(() -> { ThreadLocal<Integer> threadLocal = new ThreadLocal<>(); threadLocal.set(1); InheritableThreadLocal<Integer> inheritableThreadLocal = new InheritableThreadLocal<>(); inheritableThreadLocal.set(2); new Thread(() -> { System.out.println("threadLocal=" + threadLocal.get()); System.out.println("inheritableThreadLocal=" + inheritableThreadLocal.get()); }).start(); }, " Parent thread "); parentParent.start();}
The operation results are as follows :
The child thread gets the from the parent thread ThreadLocal The value in
The principle is as follows :
First we need to know Thread Class maintains two ThreadLocalMap

To follow up new Thread() Method
Its construction method calls init Method , init Method to inheritThreadLocals The value is set to true

Continue to follow up .
When inheritThreadLocals The value of is true And its parent thread inheritableThreadLocals Not for null when , Put its parent thread inheritableThreadLocals Assigned to the current thread inheritableThreadLocals

This is how the child thread can get the parent thread ThreadLocal The key to value .
Continue to follow up have a look InheritableThreadLocal Of get() Method
get() There's nothing to look at , Namely ThreadLocal Of get() Method .

Be careful :InheritableThreadLocal Yes ThreadLocal Of getMap() Method
ThreadLocalMap getMap(Thread t) { // Get the thread's own variables threadLocals, And bind to the member variables of the current calling thread. threadLocals On return t.threadLocals;}void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); // Create for ThreadLocalMap Of table Attribute assignment , And will firstValue At the top of the array .}
createMap Method not only creates threadLocals, The local variable value to be added is also added to the threadLocals in .
InheritableThreadLocal Class inherited ThreadLocal class , Rewrite the childValue、getMap、createMap Method .
among createMap When the method is called , Created is inheritableThreadLocal instead of threadLocals.
Empathy ,getMap Method to call the current caller thread get Method is not returned threadLocals It is inheritableThreadLocal.
Recent hot article recommends :
1.1,000+ Avenue Java Arrangement of interview questions and answers (2022 The latest version )
2. Explode !Java Xie Cheng is coming ...
3.Spring Boot 2.x course , It's too complete !
4. Don't write about the explosion on the screen , Try decorator mode , This is the elegant way !!
5.《Java Development Manual ( Song Mountain version )》 The latest release , Download it quickly !
I think it's good , Don't forget to like it + Forward !
版权声明
本文为[Java technology stack]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230059438910.html
边栏推荐
- What is the lifecycle of automated testing?
- 【以太网交换安全】--- 交换机流量控制/DHCP Snooping/IP Source Guard
- Free trial for the first three months! Borui data alarm platform onealert is in progress
- IMX6ULL裸機開發之硬件IIC分析及配置過程
- L2-002 linked list weight removal (25 points)
- Open WebRTC Toolkit(OWT) Server User Guide
- Daily practice (47): find different
- Let's talk about ruby
- 2.60 - suppose we number the bytes in a W-bit word from 0 (lowest bit) to w / 8 - 1 (highest bit). Write the code of the following C function, which will return an unsigned value, in which byte I of p
- Deloitte 2022 technology trend: it self subversion, technology cross-border integration and innovation
猜你喜欢

The common public page cannot be imported into the templates subset directory of thymeleaf, otherwise an error will be reported: template parsing error, error reason: the reference path of the public

Cross domain problem and solving cross domain problem by UMI proxy proxy
![[actf2020 freshman competition]](/img/0c/4c06112383c0b225c987a499b622a9.png)
[actf2020 freshman competition]

Im instant messaging development how to design a database that can support millions of concurrent users

Yyds dry goods counting flag variable rule

曦智科技沈亦晨入选2022达沃斯世界经济论坛“全球青年领袖”

Acrel-2000型电力监控系统在兴庆坊新兴广场配电所配电回路用电的实时监控和管理

Cloud native Virtualization: building edge computing instances based on kubevirt

Deep learning basic learning - RNN and ltsm

Jijian cloud x servicego: help hardware manufacturers realize intelligent management of equipment repair and maintenance
随机推荐
留给给5月的文章
L2-023 graph coloring problem (25 points)
【Android工程师与智能家居产品的第一次接触③】SmartConfig一键配网在硬件端的具体实现|ESP8266一键配网在Arduino的具体实现|玉念聿辉
Can you really cross with a k-fold? Thoughts on k-fold intersection
In depth report: in the heterogeneous era, chips need to integrate multiple templates
Soochow securities x kangaroo cloud: the data is easily available and has millisecond response ability. What did Soochow securities do right?
L3-1 straight beat Huanglong (30 points) DJK + DFS
What is the lifecycle of automated testing?
L2-007 family real estate (25 points) and search set or graph traversal
Open WebRTC Toolkit(OWT) Server User Guide
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
Acrel-3200远程预付费电能管理系统 在福州万宝产业园的应用
How does zhiting connect Xiaomi smart speakers?
Cloud native Virtualization: building edge computing instances based on kubevirt
IMX6ULL裸机开发之硬件IIC分析及配置过程
C language guessing game and trickery game
Application of acrel-3200 remote prepaid electric energy management system in Fuzhou Wanbao Industrial Park
leetcode 396. Rotation function
L2-021 praise crazy devil (25 points)
Free trial for the first three months! Borui data alarm platform onealert is in progress