java 守护程序线程_java守护程序线程和非守护程序线程

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值