当前位置:网站首页>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 .
边栏推荐
- [正则表达式] 匹配分组
- Network equipment hard core technology insider router 19 dpdk (IV)
- Spark 3.0 DPP implementation logic
- 【剑指offer】面试题45:把数组排成最小的数
- 折半插入排序
- Network equipment hard core technology insider router Chapter 16 dpdk and its prequel (I)
- 表格插入行内公式后,单元格失去焦点
- 线程中死锁的成因及解决方案
- Leetcode 74. search two-dimensional matrix bisection /medium
- Dan bin Investment Summit: on the importance of asset management!
猜你喜欢
随机推荐
QT (five) meta object properties
C language: factorial recursive implementation of numbers
go语言慢速入门——基本内置类型
Spark 3.0 DPP实现逻辑
JS find the maximum and minimum values in the array (math.max() method)
Implementation of spark lazy list files
【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
【云享读书会第13期】视频文件的编码格式
STL value string learning
Spark TroubleShooting整理
leetcode-1:两数之和
JS uses for in and for of to simplify ordinary for loops
Network equipment hard core technology insider router Chapter 17 dpdk and its prequel (II)
[系统编程] 进程,线程问题总结
Pytorch replaces some components in numpy. / / please indicate the source of the reprint
Network equipment hard core technology insider router Chapter 16 dpdk and its prequel (I)
Spark 3.0 testing and use
学习Parquet文件格式
Multi table query_ Exercise 1 & Exercise 2 & Exercise 3
线程中死锁的成因及解决方案








![[TensorBoard] OSError: [Errno 22] Invalid argument处理](/img/bf/c995f487607e3b307a268779ec1e94.png)
