当前位置:网站首页>面试官:ThreadLocal使用场景有哪些?内存泄露问题如何避免?
面试官:ThreadLocal使用场景有哪些?内存泄露问题如何避免?
2022-07-28 13:49:00 【InfoQ】
ThreadLocal使用场景有哪些?
ThreadLocal.ThreadLocalMap threadLocals = null;ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } return setInitialValue();} ThreadLocalMap getMap(Thread t) { return t.threadLocals;}public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value);}public void remove() { ThreadLocalMap m = getMap(Thread.currentThread()); if (m != null) m.remove(this);}ThreadLocal内存泄露问题如何避免?

- ThreadLocal 不是用于解决共享变量的问题的,也不是为了协调线程同步而存在,而是为了方便每个线程处理自己的状态而引入的一个机制。这点至关重要。
- 每个Thread内部都有一个ThreadLocal.ThreadLocalMap类型的成员变量,该成员变量用来存储实际的ThreadLocal变量副本。
- ThreadLocal并不是为线程保存对象的副本,它仅仅只起到一个索引的作用。它主要是为每一个线程隔离一个类的实例,这个实例的作用范围仅限于线程内部。
边栏推荐
- Xcode编写SwiftUI代码时一个编译通过但导致预览(Preview)崩溃的小陷阱
- Forage QR code -- online QR code generator
- C # 7 methods to obtain the current path
- Node文件操作
- Three methods to disassemble the rotation array
- UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing
- 手机滚动截屏软件推荐
- Using reflection to build a menu spanning tree
- AFNetworking速成教程
- Langjing Technology (Trax China) "robot +ai" opens the era of Chinese retail meta universe
猜你喜欢
![[ecmascript6] async and await](/img/3c/c7de42ad572dc95b188243c02dd228.png)
[ecmascript6] async and await

When Xcode writes swiftui code, it is a small trap that compiles successfully but causes the preview to crash

MiniTest--小程序自动化测试框架

@Solution to DS ('slave') multi data source compatible transaction problem
Some problems encountered in the development of Excel VBA, solutions, and continuous updates

MeterSphere--开源持续测试平台

UI开发中所遇到的各种坑

8、 Picker usage drop-down box selection effect

2022 safety officer-a certificate operation certificate examination question bank simulated examination platform operation

bgp实验
随机推荐
7.27 simulation summary
@Solution to DS ('slave') multi data source compatible transaction problem
OKR与GRAD
unittest执行runTestCase提示<_io.TextIOWrapper name=‘<stderr>‘ mode=‘w‘ encoding=‘utf-8‘>解决方案
Many "double first-class" universities have launched the research guarantee and prediction name!
Use of formdata object, VAR formdata=new formdata()
这3款在线PS工具,得试试
[线程安全问题] 多线程到底可能会带来哪些风险?
2022年熔化焊接与热切割考题及在线模拟考试
PowerDesigner creates a database model (conceptual model example)
2022高处安装、维护、拆除考试题库及在线模拟考试
IP black and white list
Analysis of thrift serialization protocol
Another way of understanding the essence of Hamming code
ZABBIX distributed
Excel VBA 免密查看VBE加密代码
Thoughts on the construction of some enterprise data platforms
Raspberry pie foundation | summarize and record some operations in the learning process of raspberry pie
FormData对象的使用, var formdata=new FormData()
OKR and grad