在JDK1.5中,java.lang.Thread类新增了一个getAllStackTraces()方法用于获取虚拟机中所有线程的StackTraceElement对象。
for(Map.Entry<Thread, StackTraceElement[]> stackTrace : Thread.getAllStackTraces().entrySet()){ Thread thread = (Thread)stackTrace.getKey(); StackTraceElement[] stack = (StackTraceElement[])stackTrace.getValue(); if(thread.equals(Thread.currentThread())){ continue; } System.out.print("\n线程:"+thread.getName()+"\n"); for(StackTraceElement element: stack){ System.out.print("\t"+element+"\n"); } }
时间: 2024-10-27 13:13:48