答:当应用程序开始运行时,会有一个守护线程,其任务是执行main()。
这是不正确的。见下文。
B.当一个应用程序开始运行时,有一个非守护进程线程,其工作是执行main()。
正确。当最后一个非守护线程退出时,JVM退出。如果主线程不是非守护程序,那么JVM将启动并看到没有非守护程序线程在运行,并且将立即关闭。
C.由守护程序线程创建的线程最初也是守护程序线程。
D.由非守护程序线程创建的线程最初也是非守护程序线程。
两者都是正确的。默认情况下,线程从产生它的线程中获取其守护程序状态。守护程序线程生成其他守护程序线程。非守护程序线程会生成其他非守护程序线程。查看来自的代码
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 thatthread.setDaemon(true);// we need to set the daemon status _before_ the thread startsthread.start();



