当前位置:网站首页>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 .
边栏推荐
- NIO、BIO、AIO
- Basic principles of spanning tree protocol
- 堆內存分配的並發問題
- Heavyweight! Fada is listed as a "specialized and new" enterprise
- Pinduoduo updates the merchant live broadcast service agreement and strictly punishes the illegal merchants
- Embedded development: tips and tricks -- clean jump from boot loader to application code
- Creating files, recursively creating directories
- Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?
- 华大04a工作模式/低功耗模式
- Code farmers should also understand the IPv4 subnet division of point networks
猜你喜欢

Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware

详细了解Redis的八种数据类型及应用场景分析

【个人实验报告】

【Mongodb】READ_ME_TO_RECOVER_YOUR_DATA,数据库被恶意删除

In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience

Docker 安装 Redis-5.0.12,详细步骤

中国SSD行业企业势力全景图

nuScenes——数据集配置过程中遇到图像文件缺失或大小为0时的补救方法

Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?

Redis hop table
随机推荐
Huada 4a0gpio settings
NIO、BIO、AIO
FANUC机器人_KAREL编程入门学习(1)
CDN principle
Fanuc robot_ Introduction to Karel programming (1)
如何提取网页中的日期?
Chapter 10 project communication management
【软件工程】期末重点
Principles of Ethernet port mirroring, link aggregation and VLAN Technology
Main steps of system test
Envoy obtain the real IP address of the client
Short video mall system, how does scroll view adapt to the remaining height of the page
See how sparksql supports enterprise level data warehouse
High level application of SQL statements in MySQL database (II)
interrupt、interrupted 、isInterrupted 区别
How to extract dates from web pages?
【軟件工程】期末重點
面试害怕被问MySQL相关问题 ?这份三万字精华总结 + 面试100 问,吊打面试官完全够了
Unable to use the bean introduced into the jar package
seven