当前位置:网站首页>Principle analysis of ThreadLocal source code
Principle analysis of ThreadLocal source code
2022-06-25 15:59:00 【Favorite grapes】
What( What is? ThreadLocal)
Commonly known as thread local variables , Is a method that can be used in the current thread , Just get The value set by this thread Tools for .
Like a string ,A The thread is set “abc”,B The thread is set “def”, When you want two threads to read again , Namely ,A Read “abc”,B Read yes “def”.
Why( Why use it ?)
Want different threads , Use the same variable , But the values read through it are the values set by each .
How( How to use it? ?)
ThreadLocal<String> tl = new ThreadLocal<>();
// thread1
tl.set("abc");
sout(tl.get()); // abc
// thread2
tl.set("def");
sout(tl.get()); // def
ThreadLocal How to do it ?
1、 Simply create one ThreadLocal
Objects are useless . Only called set()
perhaps get()
When the method is used ,ThreadLocal
It's just starting to work .
2. from set
Speaking of , When we call set
When the method is used ,ThreadLocal
Internal will get the current Thread
One inside threadLocals
object , And its essence is ThreadLocalMap
( A custom HashMap
, There's a... In it Entry
Array ,Entry
Inherited from WeakReference
,Entry
Added a value
attribute , Used to store values )
3. ThreadLocal<?>
As Entry
Value of weak reference , Is equivalent to HashMap
Of Key
.
4. So when you call get
perhaps set
When the method is used , It will go to the current thread threadlocals
Read or set the corresponding value in .
ThreadLocalMap And traditional HashMap The difference between ?
It has only one array inside , When hash collision occurs , Will get the next array index , Instead of using the zipper method to create a linked list from the back . So his architecture , It's more like an array , Only the index value is through hash
The algorithm determines .
A less elegant drawing illustrates :
边栏推荐
- Sword finger offer 04 Find in 2D array
- Write one file to the marked location of another file
- Optimization of lazyagg query rewriting in parsing data warehouse
- Continuously improve the overall performance of adaoracle Oracle Oracle
- Servlet详解
- Native JS dynamically add elements
- Cloning and importing DOM nodes
- Report on Hezhou air32f103cbt6 development board
- Load local cifar10 dataset
- Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way
猜你喜欢
Prototype mode
剑指 Offer 04. 二维数组中的查找
Source code analysis of nine routing strategies for distributed task scheduling platform XXL job
Resolve Visio and office365 installation compatibility issues
VectorDraw Developer Framework 10.1001 Crack
Describe your understanding of the evolution process and internal structure of the method area
Educational administration system development (php+mysql)
What is session? How is it different from cookies?
Why is it said that restarting can solve 90% of the problems
Sword finger offer 04 Find in 2D array
随机推荐
数据存储和传输文件之XML使用和解析详解
f_read 函数[通俗易懂]
Advanced SQL statement 1 of Linux MySQL database
Sword finger offer 09 Implementing queues with two stacks
《睡眠公式》:怎么治睡不好?
leetcode-8. String to integer (ATOI)
Create raspberry PI image file of raspberry pie
AspNetCore&云效Flow持续集成
Lombok common notes
JS中的==和===的区别(详解)
Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
揭秘GaussDB(for Redis):全面对比Codis
Several ways of SQL optimization
Binocular 3D perception (I): preliminary understanding of binocular
原生js动态添加元素
golang reverse a slice
将一个文件写入到另一个文件的标记位置
MySQL installation tutorial
Built in methods for data types
商城风格也可以很多变,DIY 了解一下!