当前位置:网站首页>Detailed explanation of ThreadLocal
Detailed explanation of ThreadLocal
2022-07-24 20:43:00 【young_ man2】
Catalog
3、 ... and 、 Why? Map Of key To set as weak reference ?
Preface
I'm reading the algorithm book today hash Table time , solve hash The conflict section mentioned this concept , So I just want to record
One 、 What is? ThreadLocal?
ThreadLocal yes Thread Local variables on , It can realize the isolation between threads , Sharing of resources within threads , Threads are private
ps: Here, the private and public threads are described as follows
![]()
Two 、 Bottom source
because ThreadLocal Is attached to Thread Of , Then we can take a look here first Thread For in the ThreadLocal The definition of
ThreadLocalMap threadLocals;
ThreadLocalMap inheritableThreadLocals;Thread One was maintained internally ThreadLocalMap Properties of , So what is ThreadLocalMap? Let's take a look at the underlying source code

ThreadLocalMap Is its static inner class , And it is default , So we cannot get this internal object externally , Only through ThreadLocal To control . And the interior is made of Entry constitute , Every Entry Array of key Is a weak reference ,value It is not .
About Entry You can see :
And then let's see ThreadLocal Internal method of :

We found that if we go get Words , No, get To the current thread map perhaps map There is no current ThreadLocal Key words , It initializes a map, also take ThreadLocal As key,null As value Initialize it ( And others map Make a difference , its get Method if get If it doesn't arrive, it will set Current TheadLocal go in , Other map The implementation has not been modified map The material in that ); If you get the map Words , Will key=ThreadLocal Of value It's worth taking out

ThreadLocalMap Some characteristics of
The initial capacity is 16, The expansion factor is 2/3, Double the expansion capacity
Use open addressing to resolve conflicts
3、 ... and 、 Why? Map Of key To set as weak reference ?

Soft citation 、 Strong quotation and weak quotation
Four 、 Value How to release ?

5、 ... and 、ThreadLocal How to realize isolation ?

ThreadLocal The reason why we can achieve variable thread isolation , In fact, each thread has its own ThreadLocalMap Object to store the same threadLocal example set Value , And the value is also based on the same threadLocal Examples go to their own ThreadLocalMap Inside looking for , Nature doesn't affect each other , So as to achieve the goal of thread isolation !
summary
ThreadLocal Belong to Thread Private !
ThreadLocalMap If it does not exist, it will be automatically created 【 Use get Method time 】
边栏推荐
- Markdown to PDF API data interface
- [training Day6] game [mathematics]
- [advanced data processing technology] data filtering, advanced data filling, initial and advanced data transformation
- Lua environment configuration
- Apache atlas version 2.2 installation
- Install MySQL 5.7.37 on windows10
- [training Day6] triangle [mathematics] [violence]
- Evolution of network IO model
- Near infrared dye cy7.5 labeling PNA polypeptide experimental steps cy7.5-pna|188re labeling anti gene peptide nucleic acid (agpna)
- The difference between token and session, this classic interview question deserves a more in-depth answer
猜你喜欢

whistle ERR_ CERT_ AUTHORITY_ INVALID

Cloud native observability tracking technology in the eyes of Baidu engineers

Opencv learning Day2

Methods of using tyrosine modified peptide nucleic acid PNA | Tyr PNA | BZ Tyr PNA | 99Tcm survivinmrna antisense peptide nucleic acid

Baidu PaddlePaddle easydl helps improve the inspection efficiency of high-altitude photovoltaic power stations by 98%

Selenium is detected as a crawler. How to shield and bypass it

API data interface for historical data of A-share index

Istio一之Envoy工作原理

API data interface of A-share transaction data
![[feature selection] several methods of feature selection](/img/ee/2f5224f97ac3090a535c9c74bc898f.png)
[feature selection] several methods of feature selection
随机推荐
Huawei set up login with account and password
Modulenotfounderror: no module named 'pysat.solvers' (resolved)
Summary of communication with customers
Appium element positioning - App automated testing
Home Assistant中接入博联WiFi智能遥控
Do you want to verify and use the database in the interface test
Unity's ugui text component hard row display (improved)
Spark related FAQ summary
Login Huawei device in SSH mode
Is there any capital requirement for the online account opening of Ping An Securities? Is it safe
Lua environment configuration
Oracle primary key auto increment setting
Five digital transformation strategies of B2B Enterprises
Native applets are introduced using vant webapp
Understand the domestic open source Magnolia license series agreement in simple terms
Generate self signed certificate: generate certificate and secret key
When using vscode, the tab indentation changes from 4 spaces to small arrows (solved)
Do you want to enroll in a training class or study by yourself?
147 set whether to cache by using the routing meta information - use of include and exclude - use of activated and deactivated
Docker builds redis and clusters
