Java Thread dumpStack()方法

2019-10-16 22:24:44

Thread類的dumpStack()方法將當前執行緒的堆疊跟蹤列印到標準錯誤流。它僅用於偵錯。

語法

public static void dumpStack()

返回

此方法不返回任何值。

範例

public class JavaDumpStackExp   
{  
    public static void main(String[] args)   
    {  
        Thread thread = Thread.currentThread();  
        thread.setName("My ThreadDumpStack");  

        // set thread priority to 6  
        thread.setPriority(6);  
        // prints the current thread  
        System.out.println("Current thread: " + thread);  

        int count = Thread.activeCount();  
        System.out.println("currently active threads: " + count);  

        // prints a stack trace of the current thread to the standard error stream, used for debugging   
        Thread.dumpStack();  
   }  
}

執行上面範例程式碼,得到以下結果:

Current thread: Thread[My ThreadDumpStack,6,main]
currently active threads: 1
java.lang.Exception: Stack trace
    at java.lang.Thread.dumpStack(Thread.java:1336)
    at yiibai.java.JavaDumpStackExp.main(JavaDumpStackExp.java:19)