threadlocal的key是强引用
时间: 2023-10-16 22:59:44 浏览: 100
ThreadLocal的用处
ThreadLocal的key是弱引用。当一个ThreadLocal没有外部强引用引用它时,系统的垃圾回收机制会回收这个ThreadLocal对象,导致ThreadLocalMap中对应的key为null。这样一来,就无法访问这些key为null的Entry的value。如果当前线程不结束,这些key为null的Entry的value就会一直存在一条强引用链,造成内存泄漏。因此,为了避免内存泄漏,ThreadLocal的key使用弱引用。\[1\]\[2\]\[3\]
#### 引用[.reference_title]
- *1* *2* [ThreadLocal之强、弱、软、虚引用](https://blog.csdn.net/weixin_43847283/article/details/125470183)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [强引用和弱引用的Threadlocal](https://blog.csdn.net/a779868946/article/details/121458153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文