A. When an application begins running, there is one daemon thread, whose job is to execute main().
这是不正确的.见下文.
B. When an application begins running, there is one non-daemon thread, whose job is to execute main().
正确.当最后一个非守护程序线程退出时,JVM退出.如果主线程不是非守护进程,那么JVM将启动并看到没有非守护进程线程在运行并立即关闭.
C. A thread created by a daemon thread is initially also a daemon thread.
D. A thread created by a non-daemon thread is initially also a non-daemon thread.
两者都是正确的.该线程默认从产生它的线程获取其守护进程状态.守护程序线程产生其他守护程序线程.非守护程序线程产生其他非守护程序线程.查看Thread.init()中的代码:
Thread parent = currentThread();
...
this.daemon = parent.isDaemon();
如果要更改守护程序状态,则必须在启动线程之前执行此操作.
Thread thread = new Thread(...);
// thread has the daemon status of the current thread
// so we have to override it if we want to change that
thread.setDaemon(true);
// we need to set the daemon status _before_ the thread starts
thread.start();