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("主线程运行结束");
}
}
执行结果不一致,为什么?