当前位置:网站首页>ThreadLocal与线程池在使用中可能会出现的两个问题
ThreadLocal与线程池在使用中可能会出现的两个问题
2022-06-21 17:51:00 【zhaozhen】
直接线程池中获取主线程或非线程池中的ThreadLocal设置的变量的值
例如
private static final ThreadPoolExecutor syncAccessPool = new ThreadPoolExecutor(
50,
80,
8000,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(600)
);
public static void main(String[] args) {
ThreadLocal<String> threadLocal = new ThreadLocal<>();
threadLocal.set("userId115807");
syncAccessPool.execute(()->{
System.out.println(threadLocal.get());
});
}
最后打印的结果是null
解决办法:真实使用中相信大家不会这么使用的,但是我出错主要是因为使用了封装的方法,封装的方法中使用了ThreadLocal,这种情况下要先从ThreadLocal中获取到方法中,再设置到线程池
线程池中使用了ThreadLocal设置了值但是使用完后并未移除造成内存飙升或OOM
public class ThreadLocalOOM {
static class LocalVariable{
private Long[] a = new Long[1024*1024];
}
private static final ThreadPoolExecutor syncAccessPool = new ThreadPoolExecutor(
50,
80,
8000,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(600)
);
final static ThreadLocal<LocalVariable> threadLocal = new ThreadLocal<LocalVariable>();
public static void main(String[] args) throws InterruptedException {
Thread.sleep(10000);
for (int i=0;i<100;i++){
syncAccessPool.execute(()->{
threadLocal.set(new LocalVariable());
System.out.println("use local variable");
});
Thread.sleep(1000);
}
System.out.println("pool execute over");
}
}
这个程序使用jconsole程序观察到的内存变化为
在使用完之后remove之后的内存变化
public static void main(String[] args) throws InterruptedException {
for (int i=0;i<100;i++){
syncAccessPool.execute(()->{
threadLocal.set(new LocalVariable());
System.out.println("use local variable");
threadLocal.remove();
});
Thread.sleep(1000);
}
System.out.println("pool execute over");
}
内存相比之前降低了几倍。这个原因就是没有remove,线程池中所有存在的线程都会持有这个本地变量,导致内存暴涨。如果将private Long[] a = new Long[1024*1024]; 扩大可能就会很快抛出OOM异常
边栏推荐
- 互联网通信流程
- 2022中国眼博会,山东青少年眼睛健康展,视力矫正与康复展
- Guys, please ask me a question about flynk SQL. I have an FQL statement, insert into C sale
- Three. JS 3D particle animation JS special effect code
- 《Go题库·9》同一个协程里面,对无缓冲channel同时发送和接收数据有什么问题
- 老师们,oracle-cdc遇到不能解析的dml语句,因为这个语句里面有个字段是比较特殊的空间地理位
- GoF模式-03-行为型模式(下)
- OGG-21.3 报错 OGG-00768 Failed to Map database character to ULibCharSet
- 外资上演“胜利大逃亡”、内资接盘,新东方在线“方”了?
- JSP Basics
猜你喜欢

中国两颗风云气象“新星”主要数据产品将向全球用户开放共享

Six steps of JDBC programming

Delete the specified screen

The main data products of China's two Fengyun meteorological "new stars" will be open and shared with global users

Literature analysis CiteSpace 6.1.2 download and installation tutorial

vivo 容器集群监控系统架构与实践

This humble doctor's thesis is very popular: looking back, I feel sorry for countless mountains

De « l'entreprise gérée par le village » au « Groupe de 10 milliards de yuans », comment l'industrie Hongxing complète - t - elle le « changement de papillon »?

一篇文章彻底学会画数据流图

缓存设计问题
随机推荐
Huawei launches new products again? These models have excellent functions
6月22日直播 | 华南理工詹志辉: 面向昂贵优化的进化计算
恒泰证券VIP低佣金开户链接安全的吗?
大佬们,麻烦问一个flink sql的问题,我有一个fql语句,insert into c sele
R语言glm函数构建二分类logistic回归模型(family参数为binomial)、使用summary函数查看模型汇总统计信息并解读特征
Hongmeng version of "Tiktok" is a great experience
品牌、产品和服务齐发力,东风日产接下来有什么新动作?
文献分析 Citespace 6.1.2 下载及安装教程
【区间和专题の前缀和】线段树(动态开点)运用题
牛客网:归并两个有序的数组
Second cloud's original fully compatible solution for Xinchuang is upgraded to help accelerate the implementation of Xinchuang industry
从“村办企业”到“百亿集团”,红星实业何以完成“蝶变”?
C# Mapster 对象映射器学习
R语言使用glm函数构建泊松回归模型(Poisson Regression)、使用coef函数获取泊松回归模型的系数并分析各个变量的效应
使用ip怎么申请ssl证书
11 brève introduction et installation de la Bibliothèque d'analyse de soup beautiful
Dao and encapsulation of entity classes
Equals null pointer exception
Internet communication process
Explanation of El table paging select all function