集册 Java实例教程 打印线程的堆栈帧的详细信息

打印线程的堆栈帧的详细信息

欢马劈雪     最近更新时间:2020-01-02 10:19:05

627
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
打印线程的堆栈帧的详细信息
/** 来自 时代Java - nowjava.com**/

public class Main {

  public static void main(String[] args) {

    m1();

  }


  public static void m1() {

    m2();

  }


  public static void m2() {

    m3();

  }


  public static void m3() {

    Throwable t = new Throwable();

    StackTraceElement[] frames = t.getStackTrace();

    printStackDetails(frames);

  }


  public static void printStackDetails(StackTraceElement[] frames) {

    System.out.println("Frame count: " + frames.length);

    /**
     from
    * 时代Java - N o w  J a v a . c o m 
    **/

    for (int i = 0; i < frames.length; i++) {

      // Get frame details

       int frameIndex = i; // i = 0 means top frame

      String fileName = frames[i].getFileName();

      String className = frames[i].getClassName();

      String methodName = frames[i].getMethodName();

      int lineNumber = frames[i].getLineNumber();


      // Print frame details

      System.out.println("Frame Index: " + frameIndex);

      System.out.println(
展开阅读全文