ThreadLocal数据隔离
时间: 2023-10-14 08:31:46 浏览: 142
ThreadLocal提供了一种以空间换时间的方式,为每个线程提供了一份变量的副本,从而实现了线程间的数据隔离。每个Thread对象都有一个ThreadLocalMap成员变量,用于存储线程的局部变量副本。每个线程在访问ThreadLocal变量时,实际上是通过ThreadLocalMap来获取副本数据。ThreadLocalMap中的key是ThreadLocal实例的弱引用,value是对应的局部变量副本。由于key是弱引用,当ThreadLocal实例被回收时,对应的局部变量副本也会被回收,从而实现了对ThreadLocal的回收。通过ThreadLocal实现的数据隔离能够让多个线程并发执行而不会相互干扰,同时解决了哈希冲突问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [ThreadLocal 线程隔离](https://blog.csdn.net/weixin_49561445/article/details/120597228)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [java ThreadLocal(应用场景及使用方式及原理)](https://blog.csdn.net/weixin_34128501/article/details/86245024)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文