ThreadMXBean tm = ManagementFactory.getThreadMXBean();
tm.setThreadContentionMonitoringEnabled(true);
long [] tid = tm.getAllThreadIds();
ThreadInfo [] tia = tm.getThreadInfo(tid, Integer.MAX_VALUE);
long [][] threadArray = new long[tia.length][2];
for (int i = 0; i < tia.length; i++) {
long threadId = tia[i].getThreadId();
long cpuTime = tm.getThreadCpuTime(tia[i].getThreadId())/(1000*1000*1000);
threadArray[i][0] = threadId;
threadArray[i][1] = cpuTime;
}
检测到如下线程问题:
Thread ID: 89
Thread Name: http-6080-Processor73
Thread State: RUNNABLE
Thread Lock Name: null
Thread Lock Owner Name: null
Thread CPU Time: 35678 sec
Stack Info: (depth:31)
+java.util.HashMap.get(HashMap.java:303)
+com.netqin.baike.server.nqrs.CloudSecurityCommand.writePkgsLog(CloudSecurityCommand.java:466)
+com.netqin.baike.server.nqrs.CloudSecurityCommand.execute(CloudSecurityCommand.java:153)
+com.netqin.baike.server.BaikeServer.service(BaikeServer.java:64)
+sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
CPU占用时间达到 35678秒 ,到下午到了50000秒左右,tomcat的CPU占用达到了200%
分析代码,发现是单例bean中使用了 hashmap 作为类对象,多线程访问时 类成员hashmap并不是线程安全的 非单例,引起了问题。更正代码后,几个月没有发现问题
来源:http://shenguanpu.blogbus.com/logs/159954429.html