当前位置:网站首页>Detailed explanation of ThreadLocal
Detailed explanation of ThreadLocal
2022-07-06 22:38:00 【Rookie nest】
One 、ThreadLocal
1、 explain
ThreadLocal Is a thread local variable . In fact, its function is very simple , That is to provide a copy of the variable value for each thread that uses the variable , yes Java A special thread binding mechanism in , Each thread can change its own copy independently , It doesn't conflict with copies of other threads .
2、 Usage method
ThreadLocal<String> nameThreadLocal = new ThreadLocal<>();
nameThreadLocal.set(name);
nameThreadLocal.get();
3、Request Use cases
stay springMvc
In a single case controller
Define member variables in request
, Use @autowired
When the injection ,request
Is the use of ThreadLocal
Thread safe .
- stay controller In the injection request yes jdk Dynamic proxy object ,ObjectFactoryDelegatingInvocationHandler Example . When we call the member domain request The method of is actually called objectFactory Of getObject() object . there objectFactory yes RequestObjectFactory.
- RequestObjectFactory Of getObject In fact, from RequestContextHolder Of threadlocal Removed from .
- Request just entered springmvc Of dispatcherServlet I will put request Related objects are set to RequestContextHolder Of threadlocal In the middle .
Reference resources : stay SpringMVC Controller In the injection Request Member domain - abcwt112 - Blog Garden
4、 Reference resources
ThreadLocal Detailed explanation
https://www.jianshu.com/p/3bb70ae81828
Two 、InheritableThreadLocal
1、 explain
ThreadLocal It is designed to bind the current thread , If you want the current thread ThreadLocal Can be used by child threads , The implementation will be quite difficult ( The user needs to pass... In the code himself ). In this context ,InheritableThreadLocal emerge as the times require .
2、 principle
When creating a new thread , The following constructor will be called .
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
We follow the init Method , You can find such a piece of code . It was created here inheritableThreadLocals .
if (inheritThreadLocals && parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
Keep looking at createInheritedMap Method , There is a new one ThreadLocalMap object , Its construction method is as follows .
Analyzing the code, we know , When creating a child thread , Will be the parent thread of inheritableThreadLocals Copy to sub thread inheritableThreadLocals object .
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++;
}
}
}
}
3、 Reference resources
InheritableThreadLocal Detailed explanation
https://www.jianshu.com/p/94ba4a918ff5
3、 ... and 、ThreadLocal Memory leak
https://blog.csdn.net/qq_25775675/article/details/105731434
边栏推荐
- 柔性数组到底如何使用呢?
- 2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
- const关键字
- 视图(view)
- Senior soft test (Information System Project Manager) high frequency test site: project quality management
- pytorch_ Yolox pruning [with code]
- Gd32f4xx serial port receive interrupt and idle interrupt configuration
- Advantages of link local address in IPv6
- 2014 Alibaba web pre intern project analysis (1)
- 机试刷题1
猜你喜欢
【LeetCode】19、 删除链表的倒数第 N 个结点
Aardio - 通过变量名将变量值整合到一串文本中
Adavit -- dynamic network with adaptive selection of computing structure
C# 三种方式实现Socket数据接收
Export MySQL table data in pure mode
pytorch_ Yolox pruning [with code]
Leetcode exercise - Sword finger offer 26 Substructure of tree
0 basic learning C language - digital tube
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
CocosCreator+TypeScripts自己写一个对象池
随机推荐
The ceiling of MySQL tutorial. Collect it and take your time
[IELTS speaking] Anna's oral learning record part1
在IPv6中 链路本地地址的优势
Use ECs to set up an agent
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
MATLAB小技巧(27)灰色预测
网络基础入门理解
Leetcode exercise - Sword finger offer 26 Substructure of tree
Is there any requirement for the value after the case keyword?
config:invalid signature 解决办法和问题排查详解
空结构体多大?
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
Slide the uniapp to a certain height and fix an element to the top effect demo (organize)
OpenSSL:适用TLS与SSL协议的全功能工具包,通用加密库
项目复盘模板
动作捕捉用于蛇运动分析及蛇形机器人开发
What are the specific steps and schedule of IELTS speaking?
雅思口语的具体步骤和时间安排是什么样的?
Mysql database basic operations DML
新手程序员该不该背代码?