当父进程退出时,Java 守护进程线程是否会自动终止? [英] Are Java daemon threads automatically killed when their parent exits?

查看:123
本文介绍了当父进程退出时,Java 守护进程线程是否会自动终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个线程创建了一个守护线程,是否可以依赖父线程退出run方法,子线程也会终止?

If a Thread creates a daemon Thread, can I rely on the fact that when the parent exits the run method, the son will also terminate?

推荐答案

否 - 线程是独立的.一个线程拥有"另一个线程并强制终止是没有意义的.

No - threads are independent. There's no sense of one thread "owning" another and forcing termination.

如果您真的询问是否当应用程序中的所有非守护线程都死了,您可以依靠进程消亡:是的,您可以.但这就是您可以依赖的全部.

If you're really asking whether when all the non-daemon threads in the application have died, you can rely on the process dying: yes, you can. But that's all you can rely on.

特别是,如果有两个非守护线程,每个都创建了一个守护线程,并且一个非守护线程终止,那么剩下的三个线程会继续运行.

In particular, if there are two non-daemon threads, each of which has created a daemon thread, and one of the non-daemon threads terminates, then the remaining three threads will continue running.

这篇关于当父进程退出时,Java 守护进程线程是否会自动终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆