当前位置:网站首页>Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
2022-07-28 14:40:00 【InfoQ】
ThreadLocal What are the use scenarios ?
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 How to avoid memory leakage ?

- ThreadLocal Not to solve the problem of shared variables , It doesn't exist to coordinate thread synchronization , It is a mechanism introduced to facilitate each thread to handle its own state . This is crucial .
- Every Thread There's one inside ThreadLocal.ThreadLocalMap A member variable of type , This member variable is used to store the actual ThreadLocal Copies of variables .
- ThreadLocal Instead of saving a copy of the object for the thread , It only serves as an index . It mainly isolates an instance of a class for each thread , The scope of this instance is only within the thread .
边栏推荐
- MeterSphere--开源持续测试平台
- 天气这么热太阳能发电不得起飞喽啊?喽啊个头……
- 3种方法解轮转数组
- Unittest executes runtestcase prompt <_ io. Textiowrapper name= '< stderr>' mode=W encoding=UTF-8 > solution
- Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
- 文件批量重命名工具Bulk Rename Utility
- Minitest -- applet automation testing framework
- 基于 MinIO 对象存储保障 Rancher 数据
- 【七夕】七夕孤寡小青蛙究极版?七夕节最终章!
- The method of implementing simple student achievement management system with C language
猜你喜欢

58子站安居,经纪人营销管理平台登录接口加密逆向

文件批量重命名工具Bulk Rename Utility

超好用的手机录屏软件推荐

ZABBIX distributed

Thoughts on the construction of some enterprise data platforms

数字化转型安全问题频发,山石网科助力数字政府建设

MQTT入门级简单介绍与使用

九、uni-popup用法 下拉框底部弹窗效果

Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it

2022低压电工考试题及答案
随机推荐
Hcip day 10
10、 Timestamp
How to use the C language library function getchar ()
Core Data 是如何在 SQLite 中保存数据的
HCIP第十天
What is gossip (E-Net gossip)
2022 high altitude installation, maintenance, removal of examination question bank and online simulated examination
SwiftUI 4.0 的全新导航系统
C语言实现简单学生成绩管理系统的方法
[ecmascript6] set and map
为自定义属性包装类型添加类 @Published 的能力
[ecmascript6] class
Excel VBA 免密查看VBE加密代码
Leetcode 1331. array sequence number conversion
基于 MinIO 对象存储保障 Rancher 数据
zabbix分布式
These three online PS tools should be tried
Node文件操作
2022 low voltage electrician examination questions and answers
Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions