当前位置:网站首页>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 .
边栏推荐
- Idea global search replace shortcut key
- Use of selector for NiO multiplexing
- 2022-06-16 工作记录--JS-判断字符串型数字有几位 + 判断数值型数字有几位 + 限制文本长度(最多展示n个字,超出...)
- How to solve the problem that the computer suddenly can't connect to WiFi
- NIO、BIO、AIO
- The core concept of JMM: happens before principle
- ThreadLocal内存泄漏问题
- NiO, bio, AIO
- Firewall working principle and detailed conversation table
- Problèmes de concurrence dans l'allocation de mémoire en tas
猜你喜欢

Yyds dry goods inventory junit5 learning II: assumptions class

Kubevela v1.2 release: the graphical operation console velaux you want is finally here

Description of software version selection of kt6368a Bluetooth dual-mode transparent chip

Web security XSS foundation 06

Seven principles of software design

Learn more about the practical application of sentinel

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

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

Ansible basic configuration

Cross border e-commerce, early entry and early benefit
随机推荐
进程的通信方式
Industrial development status of virtual human
Row and column differences in matrix construction of DX HLSL and GL glsl
华大04a工作模式/低功耗模式
Panorama of enterprise power in China SSD industry
Learn more about the practical application of sentinel
Can AI chat robots replace manual customer service?
Dynamic memory management (1)
堆内存分配的并发问题
Docker installs MySQL 8.0. Detailed steps
Docker 安装 Redis-5.0.12,详细步骤
2022-06-10 work record --js- obtain the date n days after a certain date
Leetcode: calculate the number of elements less than the current element on the right (sortedlist+bisect\u left)
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
seven
NiO zero copy
Learning bit segment (1)
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
软件设计的七大原则
Yyds dry goods inventory junit5 learning II: assumptions class