当前位置:网站首页>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 】
边栏推荐
- Processing of null value of Oracle notes
- whistle ERR_ CERT_ AUTHORITY_ INVALID
- Teach you five ways to crack the computer boot password
- What should Ali pay attention to during the interview? Personal account of Alibaba interns who passed five rounds of interviews
- [mathematical modeling / mathematical programming model]
- Hcip early summary
- vlan技术
- 147-利用路由元信息设置是否缓存——include和exclude使用——activated和deactivated的使用
- [basic data mining technology] KNN simple clustering
- PC port occupation release
猜你喜欢

How to set the allure test report

PC port occupation release

Lua environment configuration
![[msp430g2553] graphical development notes (1) configuration environment](/img/42/479c96d1e7f6747f893d1a0b65be3f.png)
[msp430g2553] graphical development notes (1) configuration environment
![[training Day10] silly [simulation] [greed]](/img/31/94c32e05b498f8ad192f8ec2c500ca.png)
[training Day10] silly [simulation] [greed]

2022 chemical automation control instrument test question simulation test platform operation

Markdown to PDF API data interface

One bite of Stream(7)

Cloud native observability tracking technology in the eyes of Baidu engineers
![[shader realizes the flicker effect of three primary colors of television signal _shader effect Chapter 5]](/img/ca/fa87bc199f3aefebddf6eb58784dc7.png)
[shader realizes the flicker effect of three primary colors of television signal _shader effect Chapter 5]
随机推荐
Acwing 94. recursive implementation of permutation enumeration
Put apples
Framework API online viewing source code
Alibaba sentinel basic operation
Solution: 2003 cant connect to MySQL server on * * * * and use near 'identified by' * * * * 'with grant option' at
Google's display of Chrome browser icons was abandoned, and it was intended to be a small rocket
Actual measurement of Qunhui 71000 Gigabit Network
Two methods of how to export asynchronous data
How to learn automated testing
Alibaba Sentinel 基操
Working principle of envy of istio I
[training Day8] tent [mathematics] [DP]
Opengl rendering pipeline
[feature transformation] feature transformation is to ensure small information loss but high-quality prediction results.
Unity's ugui text component hard row display (improved)
vlan技术
MySQL data recovery
Login Huawei device in SSH mode
Inconsistent time
Do you want to enroll in a training class or study by yourself?
