当前位置:网站首页>Two problems that may occur in the use of ThreadLocal and thread pool
Two problems that may occur in the use of ThreadLocal and thread pool
2022-06-21 19:27:00 【zhaozhen】
Get the from the main thread or non thread pool from the direct thread pool ThreadLocal Set the value of the variable
for example
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());
});
}
The final print is null
terms of settlement : In real use, I believe you won't use it like this , But I made a mistake mainly because I used the encapsulation method , The encapsulation method uses ThreadLocal, In this case, we should start from ThreadLocal Get the method in , Then set it to the thread pool
Used in thread pool ThreadLocal The value is set, but it is not removed after use, resulting in a surge in memory or 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");
}
}
This program uses jconsole The memory change observed by the program is
After use remove Subsequent memory changes
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");
}
Memory is several times lower than before . The reason is that there is no remove, All existing threads in the thread pool will hold this local variable , This led to a huge increase in memory . If you will private Long[] a = new Long[1024*1024]; Expansion may soon be thrown out OOM abnormal
边栏推荐
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.main参数指定可视化图像标题文本字体的大小
- Mvcc implementation principle of MySQL
- GoF模式-03-行为型模式(下)
- From "village run enterprise" to "ten billion group", why did red star industry complete the "butterfly transformation"?
- 【面试高频题】难度 1.5/5,经典「前缀和 + 二分」运用题
- MarkDown高级语法,兼容MarkText
- 什么是SSL证书,拥有一个SSL证书有什么好处?
- Canvas dynamic mesh background JS effect
- Wwdc22 multimedia feature summary
- 从“村办企业”到“百亿集团”,红星实业何以完成“蝶变”?
猜你喜欢
随机推荐
Excel文件加密的两种方式
2022中国眼博会,山东青少年眼睛健康展,视力矫正与康复展
全国产加固以太网交换机选择技巧
缓存设计问题
【综合笔试题】难度 2.5/5 :「树状数组」与「双树状数组优化」
期货开户的流程是什么?网上开户安全吗
鸿蒙版“抖音”,这体验感赞
11 introduction and installation of beautiful soup parsing library
老师们,oracle-cdc遇到不能解析的dml语句,因为这个语句里面有个字段是比较特殊的空间地理位
Insert class collation
Servlet规范(一)
Deep Copy
JSP 基本知识
11 Beautiful Soup 解析庫的簡介及安裝
GetEmptyBlcoksPre Info
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 »?
Summary of the 13th week
Product graphic list description layout style
Alibaba cloud Yum source configuration
Threejs aircraft Earth 3D scene animation






![[Shangshui Shuo series] day one](/img/09/ab31cc494d726e896799d21fa02502.png)
![[HCTF 2018]WarmUp](/img/b0/6baee8ac76b56378230c2218f15734.png)
