当前位置:网站首页>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
边栏推荐
- CocosCreator+TypeScripts自己写一个对象池
- 0 basic learning C language - interrupt
- The SQL response is slow. What are your troubleshooting ideas?
- That's why you can't understand recursion
- (十八)LCD1602实验
- 【编译原理】做了一半的LR(0)分析器
- Advantages of link local address in IPv6
- Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
- MySQL教程的天花板,收藏好,慢慢看
- pytorch_ Yolox pruning [with code]
猜你喜欢
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
【LeetCode】19、 删除链表的倒数第 N 个结点
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
Should novice programmers memorize code?
基於 QEMUv8 搭建 OP-TEE 開發環境
Slide the uniapp to a certain height and fix an element to the top effect demo (organize)
Clip +json parsing converts the sound in the video into text
Aardio - integrate variable values into a string of text through variable names
C# 三种方式实现Socket数据接收
在IPv6中 链路本地地址的优势
随机推荐
使用云服务器搭建代理
Financial professionals must read book series 6: equity investment (based on the outline and framework of the CFA exam)
Mysql database basic operations DML
CocosCreator+TypeScripts自己写一个对象池
【无标题】
Gd32f4xx serial port receive interrupt and idle interrupt configuration
BasicVSR_ Plusplus master test videos and pictures
NPDP认证|产品经理如何跨职能/跨团队沟通?
GD32F4XX串口接收中断和闲时中断配置
qt quick项目offscreen模式下崩溃的问题处理
Signed and unsigned keywords
NPDP certification | how do product managers communicate across functions / teams?
动作捕捉用于蛇运动分析及蛇形机器人开发
uniapp设置背景图效果demo(整理)
POJ 1258 Agri-Net
POJ 1094 sorting it all out
config:invalid signature 解决办法和问题排查详解
OpenCV VideoCapture. Get() parameter details
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
项目复盘模板