当前位置:网站首页>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
边栏推荐
- Aardio - construct a multi button component with customplus library +plus
- memcached
- Aardio - integrate variable values into a string of text through variable names
- MATLAB小技巧(27)灰色预测
- Leetcode exercise - Sword finger offer 26 Substructure of tree
- Aardio - Method of batch processing attributes and callback functions when encapsulating Libraries
- pytorch_YOLOX剪枝【附代码】
- uniapp设置背景图效果demo(整理)
- That's why you can't understand recursion
- ThreadLocal详解
猜你喜欢
CocosCreator+TypeScripts自己写一个对象池
cuda 探索
Config:invalid signature solution and troubleshooting details
视图(view)
【编译原理】做了一半的LR(0)分析器
Improving Multimodal Accuracy Through Modality Pre-training and Attention
动作捕捉用于蛇运动分析及蛇形机器人开发
Leetcode exercise - Sword finger offer 26 Substructure of tree
MySQL----初识MySQL
Aardio - 封装库时批量处理属性与回调函数的方法
随机推荐
Sizeof keyword
GD32F4XX串口接收中断和闲时中断配置
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
Aardio - does not declare the method of directly passing float values
On the problems of born charge and non analytical correction in phonon and heat transport calculations
Windows Auzre 微软的云计算产品的后台操作界面
C# 三种方式实现Socket数据接收
Aardio - 不声明直接传float数值的方法
MATLAB小技巧(27)灰色预测
柔性数组到底如何使用呢?
Adavit -- dynamic network with adaptive selection of computing structure
poj 1094 Sorting It All Out (拓扑排序)
MySQL----初识MySQL
MySQL约束的分类、作用及用法
Uniapp setting background image effect demo (sorting)
What are the interface tests? What are the general test points?
(十八)LCD1602实验
2014 Alibaba web pre intern project analysis (1)
View
【LeetCode】19、 删除链表的倒数第 N 个结点