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("主线程运行结束");
}
}
执行结果不一致,为什么?
因为junit本身就不适用于多线程调试,主线程的执行完毕会执行System.exit 直接结束进程