当前位置:网站首页>Talk about ThreadLocal
Talk about ThreadLocal
2022-07-27 15:44:00 【Hua Weiyun】
Chat ThreadLocal
Let's talk today ThreadLocal, Although less is used in development , But it is really a point often asked in interviews .
Every thread has a ThreadLocalMap,ThreadLocalMap yes ThreadLocal The static inner class of , its key yes ThreadLocal Example ,value Is the object that needs to be stored , and ThreadLocalMap In fact, it is Entry Array storage ,Entry Inherit WeakReference, It is a weak reference .
static class Entry extends WeakReference<ThreadLocal<?>> { /** The value associated with this ThreadLocal. */ Object value; Entry(ThreadLocal<?> k, Object v) { super(k); value = v; } }It needs to be popularized java Four kinds of quotations from
java Four kinds of quotations from
Strong citation : Like through new Objects are strong references , This is generally not GC Recycling
Soft citation : Only when the memory is insufficient will the memory be recycled ,
Weak reference : Whether memory is enough or not , The garbage collector will collect memory when it collects , Generally, weak references are in map Use in
Virtual reference : It can be recycled at any time
TheadLocal Threads are private , Threads are not affected , Therefore, thread safety can be guaranteed , When we transfer across layers, we can consider using ThreadLocal,spring The transaction information of is placed in ThreadLocal Medium
But everybody knows that ,ThreadLocal There is a memory leak problem , The so-called memory leak is that the memory occupied by objects that are no longer used cannot be recycled ,new Objects may cause memory leaks , When ThreadLocal When there is no strong reference ,ThreadLocalMap Of key It will be recycled , Now key by null, At this time value But strong citation , At this time, there is only the interruption or end of the current thread ,ThreadLocalMap The data in will be cleared , If the current thread has been running , obviously value Can't be recycled , A memory leak occurs . And when key When it's empty , call ThreadLocalMap Of get Methods or set Methods or remove Methods will be cleared value value
Solution
The solution is when we ThreadLocal After use , By calling ThreadLocal Of remove Method to clear data , Also can put the ThreadLocal Set to private static, such ThreadLocal It's a strong reference , such TheadLocalMap Of key Not for null, Thus, the data will be cleaned up at the appropriate time .
summary
This article mainly talks about hreadLocal Relevant knowledge points of , Including why memory leaks occur , There have been solutions to this problem , And incidentally java Four ways of reference .
边栏推荐
- Spark 本地程序启动缓慢问题排查
- Pytorch replaces some components in numpy. / / please indicate the source of the reprint
- Jump to the specified position when video continues playing
- C:什么是函数中的返回值(转)
- md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
- Modify spark to support remote access to OSS files
- go语言慢速入门——基本内置类型
- Spark Bucket Table Join
- go语言慢速入门——go运算符
- Spark 3.0 DPP implementation logic
猜你喜欢

Troubleshooting the slow startup of spark local programs

C语言:动态内存函数

Singles cup, web:web check in

/dev/loop1占用100%问题

使用Lombok导致打印的tostring中缺少父类的属性

语音直播系统——提升云存储安全性的必要手段

Spark 本地程序启动缓慢问题排查

Leetcode 74. search two-dimensional matrix bisection /medium

C语言:数据的存储

Leetcode 240. search two-dimensional matrix II medium
随机推荐
聊聊ThreadLocal
QT (five) meta object properties
Set the position of the prompt box to move with the mouse, and solve the problem of incomplete display of the prompt box
Transactions_ Basic demonstrations and transactions_ Default auto submit & manual submit
Zhaoqi scientific innovation and entrepreneurship competition planning and undertaking organization, mass entrepreneurship and innovation platform, project landing and docking
Push down of spark filter operator on parquet file
后台返回来的是这种数据,是什么格式啊
表格插入行内公式后,单元格失去焦点
股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
【剑指offer】面试题56-Ⅰ:数组中数字出现的次数Ⅰ
【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
Extended log4j supports the automatic deletion of log files according to time division and expired files
网络层的IP协议
Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
【剑指offer】面试题51:数组中的逆序对——归并排序
《吐血整理》C#一些常用的帮助类
Network equipment hard core technology insider router Chapter 21 reconfigurable router
/dev/loop1占用100%问题
Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard
Spark Bucket Table Join