如何通过Thread查看一个方法被调用的顺序

Test1

package com.dwz.concurrency.chapter11;

public class Test1 {
    private Test2 test2 = new Test2();
    
    public void test() {
        test2.test();
    }
}

Test2

package com.dwz.concurrency.chapter11;

import java.util.Arrays;
import java.util.Optional;

public class Test2 {
    public void test() {
        Arrays.asList(Thread.currentThread().getStackTrace()).stream()
          //忽略本地方法 .filter(e
-> !e.isNativeMethod()) .forEach(e-> Optional.of(e.getClassName() + ":" + e.getMethodName() + ":" + e.getLineNumber()) .ifPresent(System.out::println) ); } }

测试类

package com.dwz.concurrency.chapter11;

public class MainTest01 {
    public static void main(String[] args) {
        new Test1().test();
    }
}
原文地址:https://www.cnblogs.com/zheaven/p/12071460.html