_Mr.Tree 2023-04-17 12:48 采纳率: 100%
浏览 20
已结题

Junit调用方法和Main方法里调用结果为什么不一致?

public class Test {

    public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("org.tree.demo3.Test");
        Object o = aClass.getConstructor().newInstance();
        Method run = aClass.getMethod("run");
        run.invoke(o);
    }

    @org.junit.Test
    public void demoMain() {
        new Test().run();
    }

    public void run() {
        System.out.println("主线程[" + Thread.currentThread().getName() + "]是否Daemon: " + Thread.currentThread().isDaemon());
        Thread thread = new Thread(() -> {
            System.out.println("子线程是否Daemon: " + Thread.currentThread().isDaemon());
            System.out.println("子线程开始...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("子线程结束...");
        });
        thread.start();
        System.out.println("主线程运行结束");
    }
}

执行结果不一致,为什么?

img


img

  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-04-17 14:29
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月8日
  • 已采纳回答 4月30日
  • 创建了问题 4月17日