当前位置:网站首页>面试官: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并不是为线程保存对象的副本,它仅仅只起到一个索引的作用。它主要是为每一个线程隔离一个类的实例,这个实例的作用范围仅限于线程内部。
边栏推荐
- [translation] how to choose a network gateway for your private cloud
- C # 7 methods to obtain the current path
- pix2pix
- 2022年熔化焊接与热切割考题及在线模拟考试
- Iterator iterator interface
- 如何在 Core Data 中进行批量操作
- As a programmer, how to manage time efficiently?
- Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
- 3种方法解轮转数组
- Force deduction solution summary 1331 array sequence number conversion
猜你喜欢

朗镜科技(Trax中国)“机器人+AI”开启中国零售元宇宙时代

复制excel行到指定行

Target detection: speed and accuracy comparison (fater r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)
![[Tanabata] Tanabata lonely little frog research edition? The final chapter of Tanabata Festival!](/img/0b/4fc583a3dd4794b0c2b0d64d905be7.png)
[Tanabata] Tanabata lonely little frog research edition? The final chapter of Tanabata Festival!

QQ robot configuration record based on nonebot2

bgp实验

Forage QR code -- online QR code generator

C语言库函数getchar()怎么使用

Foundation of deep learning ---- GNN spectral domain and airspace (continuous improvement, update and accumulation)

Minitest -- applet automation testing framework
随机推荐
Foundation of deep learning ---- GNN spectral domain and airspace (continuous improvement, update and accumulation)
Raspberry pie foundation | summarize and record some operations in the learning process of raspberry pie
Factory mode and constructor mode
What is gossip (E-Net gossip)
7.27 simulation summary
Bulk Rename Utility
Custom Configuration Sections
How to effectively conduct the review meeting (Part 1)?
OKR与GRAD
[ecmascript6] set and map
【LeetCode】 贴纸拼词(动态规划)
Realization of chat room function
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
@DS('slave') 多数据源兼容事务问题解决方案
Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
C # 7 methods to obtain the current path
用友BIP CRM新品发布,赋能大中型企业营销增长
Floating point data type in C language (did you learn to waste it)
2022 melting welding and thermal cutting examination questions and online simulation examination
[translation] salt companies come to linkerd for load balancing, and stay for efficiency, reliability and performance