当前位置:网站首页>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 - integrate variable values into a string of text through variable names
- NPDP认证|产品经理如何跨职能/跨团队沟通?
- MySQL----初识MySQL
- TypeScript获取函数参数类型
- poj 1094 Sorting It All Out (拓扑排序)
- General implementation and encapsulation of go diversified timing tasks
- Gd32f4xx serial port receive interrupt and idle interrupt configuration
- Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
- 如何实现文字动画效果
猜你喜欢

Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing

【LeetCode】19、 删除链表的倒数第 N 个结点

Financial professionals must read book series 6: equity investment (based on the outline and framework of the CFA exam)

uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)

Installation and use of labelimg

Aardio - integrate variable values into a string of text through variable names

【编译原理】做了一半的LR(0)分析器

MySQL ---- first acquaintance with MySQL

云原生技术--- 容器知识点

Attack and defense world miscall
随机推荐
MySQL----初识MySQL
CocosCreator+TypeScripts自己写一个对象池
npm无法安装sharp
Return keyword
Export MySQL table data in pure mode
POJ 1258 Agri-Net
枚举与#define 宏的区别
TypeScript获取函数参数类型
GD32F4XX串口接收中断和闲时中断配置
Aardio - 封装库时批量处理属性与回调函数的方法
Typescript get function parameter type
hdu 5077 NAND(暴力打表)
网络基础入门理解
项目复盘模板
机试刷题1
Machine test question 1
Is there any requirement for the value after the case keyword?
Gd32f4xx serial port receive interrupt and idle interrupt configuration
pytorch_ Yolox pruning [with code]
2014 Alibaba web pre intern project analysis (1)