当前位置:网站首页>Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
2022-07-05 02:47:00 【It was his highness xiaoaojun】
ThreadLocal brief introduction
So first of all , We need to know what is ThreadLocal Well ? The following are the official documents ThreadLocal Brief introduction :
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).
Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).
This class provides thread local variables , These variables are different from ordinary variables , Because every thread that accesses these variables ( adopt get or set Method ) All have their own 、 Independently initialized variable copies .ThreadLocal Instances usually want to associate private static fields in a class with threads ( for example , user ID Or business ID) The associated .
As long as the thread is active and ThreadLocal Instance can access , Each thread holds an implicit reference to a copy of its thread local variable ; After the thread disappears , All copies of their thread local instances will be garbage collected ( Unless there are other references to these copies ).
In popular words, it is ,ThreadLocal A separate copy of the variable is saved for each thread , In this way, different threads only need to operate on their own variables . Thus, complex steps such as synchronization are avoided .( Very arrogant thought , As long as every thread has variables , Don't you have to synchronize those complex operations ?!)

Understand the finished ThreadLocal What is it , Under what circumstances is it suitable to use ThreadLocal Development ?
be based on ThreadLocal Its own characteristics , It is mainly applicable to modifying variables under some multithreads , But we don't want the variables to affect each other .
The most typical usage , It is used for Save the request header information of the front-end request .
Because usually , Every time the front-end request server will start a new thread for processing and transmission , Then in the request header Header information , It must only belong to this thread , Based on this point , It's easy to be with Thread Local It is combined with the feature of thread isolation .

In this case , It can ensure that each thread will get the request header information of its corresponding front-end request . Of course, there are other scenes , For example, how to distinguish whether the current request is a pressure measurement request or a normal request during pressure measurement , Both can be used ThreadLocal To implement .
ThreadLocal Principle analysis
that ,ThreadLocal How does the bottom layer implement such a structure to support its unique variables for each thread ? Next, let me show you how the underlying source code is implemented .
get Method
First let's look at get Method , The source code is as follows :
public T get() {
Thread t = Thread.currentThread(); // Get the current thread
ThreadLocalMap map = getMap(t); // Get the corresponding thread ThreadLocalMap
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this); // From this map Get the corresponding Entry structure
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value; // Generic Programming , Convert to the corresponding required type .
return result;
}
}
return setInitialValue();
}
You can see , The key point is ThreadLocalMap And the corresponding Entry structure . So what are these two things ?
static class ThreadLocalMap {
static class Entry extends WeakReference<ThreadLocal<?>> {
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
// value The corresponding is actually Thread In the ThreadLocal The next value .
value = v;
}
}
......
private Entry[] table;
......
You can see , actually ThreadLocalMap It's just one. Entry Array . And each Entry The corresponding thread is in a ThreadLocal The next value . The reason for this design is to give full consideration to the possible existence of multiple threads ThreadLocal The situation of . The specific diagram is as follows :

So that way , We can sort it out from ThreadLocal Get the logic of the value :
1、 Get the current thread Thread
2、 according to Thread Get the corresponding ThreadLocalMap
3、 from ThreadLocalMap Get the current ThreadLocal Corresponding Entry, And back to Entry The value in the .
Memory leak
One small detail that needs attention is ,ThreadLocalMap Medium Entry Objects are weak references . This may lead to a serious problem , produce Memory leak . Why is there a memory leak ? I'll borrow one here first Threadlocal General illustrations cited :

It can be seen that if the outside world is right at this time ThreadLocal The reference to is cancelled , So at this time ThreadLocal Because it's a weak quote , Will be GC To recycle , thus , We can no longer get the corresponding value The value of , This causes a memory leak .

So why use weak references ? Whether changing to strong reference can solve the problem ?
hypothesis ThreadLocal Strong references are used , So when you're right ThreadLocal After the reference of is cancelled ,ThreadLocal Because it is still ThreadLocalMap Points to , Therefore, it is impossible to recycle . The details are shown in the following figure :

Then it will still cause the problem of memory leakage , Even more “ many ”.
So obviously, essentially , Weak references are not the cause of memory leaks . actually , To avoid memory leaks , We still need our thread to take the initiative " Cut off the " quote . That is, after each use , Call the corresponding remove Method to clear .

set Method
Look again set Method , In fact, the logic is similar . I put the source code here first :
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
You can see the overall process as follows :
1、 First, get the current thread Thread.
2、 Get map Corresponding ThreadLocalMap object .
3、 Judge the present ThreadLocalMap Has it been created , If it has been created, insert the value (key yes ThreadLocal,value It's us set Value ).
4、 Otherwise, proceed map The initialization .
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
table = new Entry[INITIAL_CAPACITY]; // Initialize array , Length default 16
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1); // Calculate the corresponding hash subscript
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY); // Set the expansion threshold
}
I also put the key code of initialization here ~ If you can understand the above knowledge almost , Then the initialization here must be no problem ~.
ThreadLocal Analysis of advantages and disadvantages
advantage
1、 For common locking to ensure mutual exclusion, solve the problem of multi-threaded access ,ThreadLocal Put forward new ideas , Use the method of assigning variables to each thread to solve the conflict . The performance is relatively good .
2、 The modification of variable data by each thread is independent , No interaction .
shortcoming
1、 Pay attention to calling after use remove Method to clear the corresponding reference , Avoid memory leaks .
2、 because ThreadLocal The mechanism of action is bound to threads , therefore It cannot be directly used to save the data of asynchronous tasks , It needs to be operated in some other way . About how to realize , Please look forward to my subsequent articles hahaha ~
If you see this , Give me a compliment 、 Click a collection , If only I could pay attention ~
It's not easy to create , Thank you for your support ~
reference
When are weak references recycled _ interviewer :ThreadLocal Why memory leaks occur ?
边栏推荐
- When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
- Hmi-30- [motion mode] the module on the right side of the instrument starts to write
- GFS distributed file system
- Open source SPL optimized report application coping endlessly
- Azkaban安装部署
- Scientific research: are women better than men?
- Hmi-31- [motion mode] solve the problem of picture display of music module
- Devtools的簡單使用
- Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
- [Yu Yue education] National Open University autumn 2018 8109-22t (1) monetary and banking reference questions
猜你喜欢

端口,域名,协议。

Zabbix

Bumblebee: build, deliver, and run ebpf programs smoothly like silk

Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords

Character painting, I use characters to draw a Bing Dwen Dwen

Exploration of short text analysis in the field of medical and health (II)

Yyds dry goods inventory intelligent fan based on CC2530 design

8. Commodity management - commodity classification

Unpool(nn.MaxUnpool2d)

Cut! 39 year old Ali P9, saved 150million
随机推荐
Last week's hot review (2.7-2.13)
Hmi-31- [motion mode] solve the problem of picture display of music module
How to make OS X read bash_ Profile instead of Profile file - how to make OS X to read bash_ profile not . profile file
2022/02/13
GFS distributed file system
The database and recharge are gone
为什么腾讯阿里等互联网大厂诞生的好产品越来越少?
Master Fur
College Students' innovation project management system
【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)
Moco V2 literature research [self supervised learning]
Spark SQL learning bullet 2
Apache Web page security optimization
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
【LeetCode】501. Mode in binary search tree (2 wrong questions)
Avoid material "minefields"! Play with super high conversion rate
Design of KTV intelligent dimming system based on MCU
Yolov5 model training and detection
Acwing game 58 [End]
Android advanced interview question record in 2022