当前位置:网站首页>ThreadLocal local thread
ThreadLocal local thread
2022-06-24 22:42:00 【Passerby Chen】
ThreadLocal Local thread
ThreadLocal Local thread , That is, each thread can have its own copy of variables , Other threads cannot access their own internal
Copy variable.ThreadLocal Is a weak reference .1.ThreadLoca It's a thread local variable , In each thread that accesses the variable , An independent variable copy is initialized inside the thread , Only this thread can access 【get() or set()】 Access your own internal
Copy variable,ThreadLocal Instances are usually declared as private static.2. The thread is alive and ThreadLocal When an instance can be accessed , Each thread implicitly holds a copy of the thread local variable , When the thread life cycle ends ,ThreadLocal A copy of the instance of is lost along with the thread , By GC Garbage collection ( Unless there are other references to these copies )
ThreadLocal effect : Be sure to run under the same thread , Multiple methods called , They can share the same data .

ThreadLocal Of API
set(T value): Set the value in the current copy of the thread local variable to the specified value
get(): Returns the value in the current copy of this thread local variable
initialValue(): Returns the initial value in the current copy of this thread local variable
remove(): Remove the value from the current copy of this thread local variable
ThreadLocal Of set()

ThreadLocal Of get()


ThreadLocal Of initialValue()

ThreadLocal Of remove()

ThreadLocalMap Of Entry Entity class
1. Every Thread There is a... Inside the thread Map(ThreadLocalMap)
2.Map Stored in the collection Entry Entity class :ThreadLocal object (key) and The variable copy of the thread (value) , (key yes Current thread ThreadLocal,value The value is that we go to ThreadLocal Value stored inside ).
3.Thread Inside thread Map By ThreadLocal Maintenance of , from ThreadLocal Responsible for providing Map Get and set the variable value of thread .
4. For different threads , Every time you get a copy value , Other threads cannot get the copy value of the current thread , It forms the isolation of copies , Mutual interference .

Why use ThreadLocal Weak reference ?
If you find that entry Of key by null, I'll use a new one entry Cover up the old entry.
Under what circumstances key It will be equal to null? Namely ThreadLocal There's no after use remove, Weak references will be automatically cleaned up at the next garbage collection , This is the time key Is equal to null 了 . Then through the above code , Can overwrite these useless garbage objects , Increase security .
// ThreadLocal Local thread
private static ThreadLocal<T> threadLocal= new ThreadLocal<T>() ThreadLocal Two main application areas of :
1. Solve the concurrency problem :
The essence of solving concurrency problems is : A kind of The idea of exchanging space for time , Time efficiency has improved , But there is also a potential overflow risk when using memory .
2. Solve the problem of data storage :
The problem of data storage mainly refers to : How to realize data exchange and sharing among multiple components in the system , The thread as the executor is the most suitable data carrier . Although various components can realize data sharing , But the data is isolated from each other .
Thread synchronization mechanism is that multiple threads share the same variable , and ThreadLocal Is to create a separate copy of the variable for each thread , Each thread can change its own copy of variables without affecting the corresponding copies of other threads .
synchronized Time for space , Queue different threads to access ;
ThreadLocal It's space for time , A variable is set for each thread .
边栏推荐
- 详细了解Redis的八种数据类型及应用场景分析
- 详细了解关于sentinel的实际应用
- 磁盤的結構
- Nuscenes -- remedies for missing image files or 0-size images encountered during dataset configuration
- YGG 近期游戏合作伙伴一览
- Learn more about the practical application of sentinel
- In the multi network card environment, the service IP registered by Nacos is incorrect, resulting in inaccessible services
- How to solve the problem that the computer suddenly can't connect to WiFi
- [personal experiment report]
- interrupt、interrupted 、isInterrupted 区别
猜你喜欢

How to automatically remove all . orig files in Mercurial working tree?

How to compare two or more distributions: a summary of methods from visualization to statistical testing

揭秘B站,程序员穿女装敲代码,效率更高是真的吗?

结构体的内存对齐

In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
How to solve the problem that the computer suddenly can't connect to WiFi

Docker installs redis-5.0.12. Detailed steps

2022-06-16 工作记录--JS-判断字符串型数字有几位 + 判断数值型数字有几位 + 限制文本长度(最多展示n个字,超出...)

Heavyweight! Fada is listed as a "specialized and new" enterprise

Fanuc robot_ Introduction to Karel programming (1)
随机推荐
Row and column differences in matrix construction of DX HLSL and GL glsl
Ideal L9, new trend of intelligent cockpit
Redis-跳表
YGG recent game partners list
The ktp900f mobile download program of the fail safe mobile panel prompts that the download cannot be performed, and the target device is running or not in the transmission mode
Code farmers should also understand the IPv4 subnet division of point networks
[personal experiment report]
NIO 零拷贝
CSRF and SSRF for web attacks
Information update on automatic control principle
In the era of full programming, should I give up this road?
Description of transparent transmission function before master and slave of kt6368a Bluetooth chip, 2.4G frequency hopping automatic connection
Extend your kubernetes API with aggregated apiserver
【软件工程】期末重点
How to automatically remove all . orig files in Mercurial working tree?
LeetCode Algorithm 剑指 Offer II 027. 回文链表
Envoy obtain the real IP address of the client
Chapter 10 project communication management
Docker 安装 MySQL 8.0,详细步骤
img2pdf