简体   繁体   English

成为守护程序线程意味着什么?

[英]What does it mean to be a daemon thread?

I'm fully aware of the impact of flagging a thread as a daemon thread on JVM exit (if all non-daemon threads exit, the JVM will exit.) 我完全了解将一个线程标记为JVM出口上的守护程序线程的影响(如果所有非守护程序线程都退出,JVM将退出。)

Are there other side-effects to setting a thread as a daemon? 将线程设置为守护进程是否还有其他副作用?

(as a complete aside, which is really irrelevant except for semantics, is the meaning of a daemon thread not backwards in this case: shouldn't a daemon thread keep the JVM alive...for instance, if I wrote my own HTTP daemon and started it running, wouldn't I want the JVM to remain alive as long as that thread is alive? Or is there some other semantic for the term 'daemon'?) (作为一个完整的旁边,除了语义之外真的无关紧要的是守护程序线程在这种情况下不倒退的意思:守护程序线程不应该使JVM保持活动状态...例如,如果我编写自己的HTTP守护程序并且开始运行,只要该线程还活着,我不希望JVM保持活动吗?或者术语'守护进程'还有其他语义吗?)

As you have it, it means that the thread is detached and will run as long as the JVM runs, unless you stop it explicitly. 正如您所知,这意味着线程已分离并且只要JVM运行就会运行,除非您明确地停止它。 By spec, the JVM keeps running as long as any non -daemon thread is running. 根据规范,只要任何守护程序线程正在运行,JVM就会一直运行。

Beyond that, it doesn't mean much of anything. 除此之外,它并不意味着什么。

You could argue that this is backwards — after all, shutdown(8) kills the daemon processes — but the JVM should act like a well-behaved process otherwise, which means people should be able to expect it to terminate without having to be killed. 你可能会认为这是倒退 - 毕竟, shutdown(8)会杀死守护进程 - 但是JVM应该像一个表现良好的进程,这意味着人们应该能够期望它终止而不必被杀死。 So this spec is a design decision based on what the more common use case will be. 因此,此规范是基于更常见用例的设计决策。

A daemon thread is a service to other threads. 守护程序线程是对其他线程的服务。 Its only a daemon for the jvm itself not for the outside world. 它只是jvm本身的守护进程而不是外部世界。 thus if only daemons excist they have no more use. 因此,如果只有守护进程激动,他们就没有更多的用处。 If you want an http daemon you just write an httpserver with normal threads and invoke it as a daemon using the toolset of your os to run apps as a daemon. 如果你想要一个http守护进程,你只需要编写一个带有普通线程的http服务器,并使用你的工具集将它作为守护进程调用,以便将应用程序作为守护进程运行。

To conlcude a daemon thread in jvm is not a daemon thread for the os. 在jvm中包含守护程序线程不是os的守护程序线程。 Daemons are usually infinite loops and you dont want your jvm to go on infinitely while all you have running are a few daemons thats wait for input(which is not given). 守护进程通常是无限循环,你不希望你的jvm无限地继续运行,而你所有运行的是一些等待输入的守护进程(没有给出)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM