当前位置:网站首页>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 Chapter 21 reconfigurable router
- Troubleshooting the slow startup of spark local programs
- C语言:函数栈帧
- 【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
- 【剑指offer】面试题49:丑数
- STL value string learning
- Spark 本地程序启动缓慢问题排查
- 使用Prometheus监控Spark任务
- NPM install error unable to access
- UDP 的报文结构和注意事项
猜你喜欢

后台返回来的是这种数据,是什么格式啊

【剑指offer】面试题45:把数组排成最小的数

Spark TroubleShooting整理

md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
![[TensorBoard] OSError: [Errno 22] Invalid argument处理](/img/bf/c995f487607e3b307a268779ec1e94.png)
[TensorBoard] OSError: [Errno 22] Invalid argument处理

Push down of spark filter operator on parquet file

Troubleshooting the slow startup of spark local programs

Spark 任务Task调度异常分析

C语言:数据的存储

Binder初始化过程
随机推荐
Complexity analysis
修改 Spark 支持远程访问OSS文件
Binder initialization process
低代码是开发的未来吗?浅谈低代码平台
【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
“router-link”各种属性解释
Network equipment hard core technology insider router 20 dpdk (V)
Database: use the where statement to retrieve (header song)
Go language slow start - Basic built-in types
Leetcode 781. rabbit hash table in forest / mathematical problem medium
Go language slow start -- go operator
语音直播系统——提升云存储安全性的必要手段
【云享读书会第13期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
C:浅谈函数
C语言中交换两数的方法
传美国政府将向部分美企发放对华为销售许可证!
HaoChen CAD building 2022 software installation package download and installation tutorial
Spark 3.0 DPP实现逻辑
What format is this data returned from the background