dumpStack()方法是Thread类的静态方法,可以用于打印或显示当前线程的堆栈跟踪到System.err。dumpStack()方法的目的基本上是用于调试,并且在内部,该方法调用Throwable类的printStackTrace()方法。该方法不会引发任何异常。
public static void dumpStack()
public class ThreadDumpStackTest { public static void main(String args[]) { Thread t = Thread.currentThread(); t.setName("ThreadDumpStackTest"); t.setPriority(1); System.out.println("Current Thread: " + t); int count = Thread.activeCount(); System.out.println("Active threads: " + count); Thread threads[] = new Thread[count]; Thread.enumerate(threads); for (int i = 0; i < count; i++) { System.out.println(i + ": " + threads[i]); } Thread.dumpStack(); } }
Current Thread: Thread[ThreadDumpStackTest,1,main] Active threads: 1 0: Thread[ThreadDumpStackTest,1,main] java.lang.Exception: Stack trace at java.lang.Thread.dumpStack(Thread.java:1336) at ThreadDumpStackTest.main(ThreadDumpStackTest.java:14)
以上是在Java中使用dumpStack()方法的目的是什么?的详细内容。更多信息请关注PHP中文网其他相关文章!