Java Thread类的静态void dumpStack()方法及示例
ThreadClassstaticvoiddumpStack()
包java.lang.Thread.dumpStack()中提供了此方法。
此方法用于打印或显示当前线程到System.err(标准错误流)的堆栈跟踪。
此方法的目的基本上是用于调试(即,如果我们调用多个方法,那么很难发现错误,因此借助该方法,我们可以在堆栈跟踪或堆栈层次结构中找到错误)。
此方法是静态的,因此也可以使用类名访问此方法,例如Thread.dumpStack()。
此方法的返回类型为void,它不返回任何内容。
此方法不会引发任何异常。
语法:
    static void dumpStack(){
    }参数:
我们不会在File方法中将任何对象作为参数传递。
返回值:
此方法的返回类型为void,它不返回任何内容。
Java程序演示dumpStack()方法示例
/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/
import java.lang.Thread;
public class PrintStackTraceOfCurrentThread {
    public static void main(String[] args) {
        //通过使用currentThread()Thread类将返回 
        //当前正在执行的线程的引用。
        Thread th = Thread.currentThread();
        //通过使用setName()方法,我们设置名称 
        //当前执行线程的数量
        th.setName("Main Thread");
        //通过使用setPriority()方法,我们设置 
        //当前执行线程的优先级
        th.setPriority(2);
        //显示当前执行线程
        System.out.println("Currently Executing Thread is :" + th);
        int active_thread = Thread.activeCount();
        //显示当前线程线程组中活动线程的数量
        System.out.println("The Current active threads is : " + active_thread);
        //显示当前线程的堆栈跟踪 
        //到System.err(标准错误流)
        Thread.dumpStack();
    }
}输出结果
E:\Programs>javac PrintStackTraceOfCurrentThread.java
E:\Programs>java PrintStackTraceOfCurrentThread
Currently Executing Thread is :Thread[Main Thread,2,main]
The Current active threads is : 1
java.lang.Exception: Stack trace
        at java.lang.Thread.dumpStack(Thread.java:1365)
        at PrintStackTraceOfCurrentThread.main(PrintStackTraceOfCurrentThread.java:24)