当前位置:网站首页>What the hell is ThreadLocal doing?
What the hell is ThreadLocal doing?
2022-07-24 00:55:00 【wzf6667】
threadlocal Use
Treadlocal brief introduction
Usually , The variables we create can be accessed and modified by any thread . If you want to realize that each thread has its own unique local variables, how to solve it ? JDK Provided in ThreadLocal Class is to solve this problem . ThreadLocal The main solution of class is to let each thread bind its own value , Can be ThreadLocal A class image is a box for storing data , The box can store the private data of each thread .
If you create a ThreadLocal Variable , Then each thread accessing this variable will have a local copy of this variable , This is also ThreadLocal The origin of variable names . They can use get() and set() Method to get the default value or change its value to the value of the copy saved by the current thread , This avoids thread safety issues .
It's like having a public notebook , Anyone can write something on it . But some people are not satisfied , He just wants his own things on his notebook , Everyone has a notebook
Treadlocal principle
The simplest idea is to Threadlocal Create a thread safe map surface ,thread As key,set The value entered as value, Each thread finds its own value. However, the following method is used in actual table creation .
Thread There's a parameter called threadlocals The variable of , He is ThreadLocalMap Type of .ThreadLocalMap It can be regarded as a thread safe hashmap, yes threadlocal A static inner class of . It means that each thread has a name threadlocals Of map,key yes threadlocal,value you set In the object. and threadlocal It's the equivalent of threadlocalmap An encapsulation of . Every time set and get Values are from this thread map find key by threadlocal Of value.
Comparison of the two methods
After this design, each Map Of Entry The number is getting smaller : It used to be Thread The number of , Now it is ThreadLocal The number of , Can improve performance ;
When Thread After destruction, the corresponding ThreadLocalMap Then it was destroyed , Can reduce memory usage .
One should be built on threadlocal The big watch is divided into many small watches , In every thread . These small tables will die with the death of threads .
ThreadLocal Memory leak problem
ThreadLocalMap Used in key by ThreadLocal The weak references , and value Is a strong quote . therefore , If ThreadLocal Without strong external reference , When it comes to recycling ,key It's going to be cleaned up , and value It won't be cleaned up . thus ,ThreadLocalMap Will appear in key by null Of Entry. If we don't do anything ,value Can never be GC Recycling , At this time, there may be a memory leak .ThreadLocalMap This situation has been considered in the implementation , Calling set()、get()、remove() Method time , Will clean up key by null The record of . Use up ThreadLocal After the method It's better to call... Manually remove() Method
边栏推荐
- VIM common commands
- MariaDB database upgrade version
- Tutorial on the principle and application of database system (049) -- MySQL query (XI): sub query
- Method of C language annotation
- 暑假第四周总结
- 如何使用 SAP Intelligent Robotic Process Automation 自动操作 Excel
- Idea hot deployment (hot load)
- 多源文件方式去访问全局变量的方式(extern用法)
- 测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)
- QT入门篇(2.1初入QT的开始第一个程序)
猜你喜欢

Classic example of C language - loan balance

Database connection pool & dbutils

Design details related to sap e-commerce cloud Spartacus UI store

PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)

How to use SAP intelligent robotic process automation to automate Excel

黑马程序员-接口测试-四天学习接口测试-第四天-Postman读取外部数据文件,读取数据文件数据,iHRM项目实战,员工管理模块,添加员工,批量运行测试用例,生成测试报告,

docker mysql

How to use mitmproxy to get data return in automated testing?

Programmeur de cheval noir - test d'interface - test d'interface d'apprentissage de quatre jours - jour 4 - Postman lit des fichiers de données externes, lit des données de fichiers de données, IHRM P

1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
随机推荐
网络系统实验:ping不通的问题解决
IDEA 热部署(热加载)
Classic examples of C language - adding two scores
Project scenario: NVIDIA SMI unable to datemine the device handle for GPU 0000:01:00.0: unknown error
The high-quality digital collection of guochuang's "children's song line" is on sale, and you are invited to create a young martial arts Jianghu dream
Tutorial on principles and applications of database system (047) -- MySQL query (IX): connection query
Tutorial on principles and applications of database system (041) -- MySQL query (III): setting query conditions
This is a big problem
Network system experiment: solve the problem of Ping failure
Solve the problem that MySQL inserts Chinese garbled code into the table
GLIB-CRITICAL g_ file_ test:assertion ‘filename != null‘ failed
There are various signs that apple is expected to support AV1
First knowledge of C language functions
NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘
Xilinx FPGA one way clock input two PLLs
Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes
How to use mitmproxy to get data return in automated testing?
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
vim常用命令