为什么ShutdownHookThread“setDaemon true”

发布于 2024-12-09 14:37:43 字数 686 浏览 3 评论 0原文

我最近需要向我拥有的 Scala 应用程序添加一个关闭钩子,我发现 Scala 为此提供了一个帮助程序 ShutdownHookThread。在其源代码中,我注意到 它将新线程设置为守护线程

def apply(body: => Unit): ShutdownHookThread = {
  val t = new ShutdownHookThread(hookName()) {
    override def run() = body
  }
  t setDaemon true  // <--------- right here
  runtime addShutdownHook t
  t
}

为什么要这样做?在我看来,您可能希望在关闭钩子线程中执行相反的操作(即确保该线程在关闭 jvm 之前退出)。或者守护进程/非守护进程与关闭挂钩不相关吗?

I recently needed to add a shutdown hook to a Scala app I have, and I discovered that Scala provides a helper for this called ShutdownHookThread. In its source I noticed that it sets the new thread to be a daemon thread.

def apply(body: => Unit): ShutdownHookThread = {
  val t = new ShutdownHookThread(hookName()) {
    override def run() = body
  }
  t setDaemon true  // <--------- right here
  runtime addShutdownHook t
  t
}

Why is this done? It seems to me you'd probably want the opposite in a shutdown hook thread (i.e. make sure that thread exits before shutting down the jvm). Or is daemon/not-daemon not relevant for shutdown hooks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小鸟爱天空丶 2024-12-16 14:37:43

在 JVM 上,通常非守护线程会阻止 JVM 终止。一旦不再有任何非守护线程,JVM 将通过启动关闭来正常终止。请参阅 addShutdownHook javadoc 了解更多信息。

一旦启动关闭,我不确定守护进程状态是否重要。此外,在启动关闭之前,不会启动关闭挂钩线程。因此,在这种情况下,t setDaemon true 可能是不必要的,但也没有什么坏处。

简而言之,“守护进程”语义与 unix 不同(在 unix 领域,它表示持续运行的线程)。

On the JVM, in general a non-daemon thread will prevent the JVM from terminating. Once there are no longer any non-daemon threads, then the JVM will gracefully terminate by initiating shutdown. See the addShutdownHook javadoc for more info.

Once shutdown has been initiated, I'm not sure daemon status matters. Also shutdown hook threads aren't started until the shutdown has been initiated. So in this case t setDaemon true may be unnecessary, but it won't hurt either.

So in short the "daemon" semantic differs from unix (where in unix land it denotes a thread that keeps running).

清醇 2024-12-16 14:37:43

在这里回答我自己的问题。

两个部分:

  1. 为什么 ShutdownHookThread 使其新线程 daemon=true?
  2. 如果关闭钩子线程 daemon=true,会发生什么?

答案:

  1. 这源于“Scala 脚本”的要求(运行 scala myfile.scala 而不是首先显式编译)。 此处进行讨论。现在已更改(commit),因此 ShutdownHookThread 的未来版本 不会有此代码。
  2. 我还没有发现任何决定性的东西,但通过实验似乎并不重要。我认为这是有道理的,因为守护进程状态会影响 JVM 何时开始关闭,因此在关闭已经开始之后,守护进程状态应该不重要。

Answering my own question here.

Two parts:

  1. Why does ShutdownHookThread make its new threads daemon=true?
  2. If a shutdown hook thread is daemon=true, what happens?

Answers:

  1. This stemmed from requirements for "Scala scripting" (running scala myfile.scala rather than explicitly compiling first). Discussion here. It has now been changed (commit), so future versions of ShutdownHookThread won't have this code.
  2. I haven't found anything decisive, but experimentally it seems not to matter. I think this makes sense since daemon status affects when the JVM will commence shutdown, so after shutdown's already underway, daemon status shouldn't matter.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文