java 打印调用堆栈的2种方法:

java 打印调用堆栈的2种方法:

  1. RuntimeException
  2. Thread.currentThread().getStackTrace()
package ziegler.learn.debug;

import java.util.stream.Stream;

public class RunTest {

    void printTrackTest() {
        // 1.打印调用堆栈
        RuntimeException e = new RuntimeException("print stacktrace");
       // e.fillInStackTrace();

        System.out.println("1.打印调用堆栈");
        Stream.of(e.getStackTrace()).forEach(System.out::println);

        // 2.打印调用堆栈
        System.out.println("2.打印调用堆栈");
        Stream.of(Thread.currentThread().getStackTrace()).forEach(System.out::println);
    }
}

 

你可能感兴趣的:(Java)