当前位置:网站首页>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 】
边栏推荐
- findContours
- The maximum number of expressions in ora-01795 list is 1000
- Modulenotfounderror: no module named 'pysat.solvers' (resolved)
- How to use named slots
- Career development suggestions shared by ten CIOs
- Chrome realizes automated testing: recording and playback web page actions
- Istio一之Envoy工作原理
- Synthesis route of ALA PNA alanine modified PNA peptide nucleic acid | AC ala PNA
- [summary of Feature Engineering] explain what features are and the steps of feature engineering
- Applet wonderful bug update~
猜你喜欢

Understand the domestic open source Magnolia license series agreement in simple terms

A new UI testing method: visual perception test

How does starknet change the L2 landscape?

The difference between map and flatmap in stream
![[feature transformation] feature transformation is to ensure small information loss but high-quality prediction results.](/img/ad/6f5d97caa3f5163197ba435276a719.png)
[feature transformation] feature transformation is to ensure small information loss but high-quality prediction results.

Selenium is detected as a crawler. How to shield and bypass it
![[training Day8] tent [mathematics] [DP]](/img/d3/42869ed5bb7c9148d9fa7367a9af02.png)
[training Day8] tent [mathematics] [DP]
![[basic data mining technology] exploratory data analysis](/img/0c/1b73098a589a2af398dd3cd9d62cd7.png)
[basic data mining technology] exploratory data analysis
![[training Day6] dream [priority queue] [greed]](/img/1b/309b53618b8a116862971799ce4e78.jpg)
[training Day6] dream [priority queue] [greed]

Summary of yarn capacity scheduler
随机推荐
Eight transformation qualities that it leaders should possess
Generate self signed certificate: generate certificate and secret key
[msp430g2553] graphical development notes (2) system clock and low power consumption mode
How to apply Po mode in selenium automated testing
[shader realizes the flicker effect of three primary colors of television signal _shader effect Chapter 5]
[training Day6] dream [priority queue] [greed]
Make Huawei router into FTP server (realize upload and download function)
Solution: 2003 cant connect to MySQL server on * * * * and use near 'identified by' * * * * 'with grant option' at
[training Day6] triangle [mathematics] [violence]
Press Ctrl to pop up a dialog box. How to close this dialog box?
Appium obtains and clicks coordinates, which is very easy to use when elements are inconvenient to locate
Each blogger needs to ask himself seven basic questions
Click the button to return to the top smoothly
Selenium is detected as a crawler. How to shield and bypass it
OpenGL (1) vertex buffer
Inconsistent time
Selenium uploads files in more ways than you think
Teach you five ways to crack the computer boot password
Fluoronisin peptide nucleic acid oligomer complex | regular active group alkyne, SH thiol alkynyl modified peptide nucleic acid
Software testing interview tips | if you don't receive the offer, I'll wash my hair upside down
