首页 > 编程语言 > 详细

java get all threadlocal from thread

时间:2020-01-05 18:26:58      阅读:102      评论:0      收藏:0      [点我收藏+]

 

 

public void mytest() {
long start = System.currentTimeMillis();
Thread thread = Thread.currentThread();

Field threadLocalsField = null;
try {
threadLocalsField = Thread.class.getDeclaredField("threadLocals");

threadLocalsField.setAccessible(true);

Class threadLocalMapKlazz = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
Field tableField = threadLocalMapKlazz.getDeclaredField("table");
tableField.setAccessible(true);

Object table = tableField.get(threadLocalsField.get(thread));

int threadLocalCount = Array.getLength(table);
StringBuilder sb = new StringBuilder();
StringBuilder classSb = new StringBuilder();


int leakCount = 0;

for (int i = 0; i < threadLocalCount; i++) {
Object entry = Array.get(table, i);
if (entry != null) {
Field valueField = entry.getClass().getDeclaredField("value");
valueField.setAccessible(true);
Object value = valueField.get(entry);
if (value != null) {
classSb.append(value.getClass().getName()).append(", ");

} else {
classSb.append("null, ");
}
leakCount++;
}
}

sb.append("possible ThreadLocal leaks: ")
.append(leakCount)
.append(" of ")
.append(threadLocalCount)
.append(" = [")
.append(classSb.substring(0, classSb.length() - 2))
.append("] ");

log.warn(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
long cost = end - start;
log.info("获取ThreadLocal耗时:{}", cost);



结果: possible ThreadLocal leaks: 9 of 16 = [java.util.Collections$SynchronizedMap, null,
java.lang.Boolean, java.lang.Boolean, null, java.lang.Integer, java.lang.Boolean, brave.propagation.TraceContext, null] 

java get all threadlocal from thread

原文:https://www.cnblogs.com/zhangzhi19861216/p/12152973.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!