码迷,mamicode.com
首页 > 编程语言 > 详细

java get all threadlocal from thread

时间:2020-01-05 19:12:32      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:for   append   print   obj   else   red   tde   object   str   

 

 

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

标签:for   append   print   obj   else   red   tde   object   str   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!