如果我有一个 Thread
对象,我可以在它上面调用 setDaemon(true)
来标记这个线程不应该阻止应用程序关闭,如果所有其他非守护线程已经终止。
是否可以让这种行为自动渗透到所有子线程? IE。如果我有一个标记为守护线程的线程,是否有某种方法可以强制此线程生成的所有线程也自动标记为守护线程?
最佳答案
Is it possible to make this behavior automatically trickle down to all child-threads?
您不需要这样做,因为它是默认设置:
The newly created thread is initially marked as being a daemon thread if and only if the thread creating it is currently marked as a daemon thread. The method setDaemon may be used to change whether or not a thread is a daemon.
参见 this .
关于java - 将 "daemon"-status 传播到 Java 中的所有子线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17760951/