4

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?

overthink
  • 23,985
  • 4
  • 69
  • 69
  • No idea why, this as a default in a framework is a bad idea. You are correct to question it, for normal Java language ShutdownHooks are not daemonized. But I'd agree with the consideration to daemonize should be an application developer choice to make (if he knows best about what this thread is trying to achieve and really wants that behavior, I don't care if I run). So to be sure this is 100% a bug you'd need clarification on if/how ShutdownHooks are seralized on shutdown by the JVM. If they serialize run/join then setting Daemonize is always a bug. – Darryl Miles Oct 14 '11 at 14:22

2 Answers2

5

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).

huynhjl
  • 41,520
  • 14
  • 105
  • 158
  • Thanks for the answer. You say "in general a daemon thread will prevent the JVM from terminating". My understanding is the opposite (based on [this](http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html)). If you have only daemon threads left in an app, the JVM will exit. So if your main (non-daemon) thread ends and your shutdown hooks are daemon=true, you may exit before they run. Unless none of this matters once shutdown is initiated. – overthink Oct 14 '11 at 14:37
  • @overthink, yeah a total typo. just fixed so now first sentence is actually consistent with the second sentence! thanks for the correction. With respect to your last sentence, that's indeed what I think. "none of this matters once shutdown is initiated". I guess it would have to be tested or googled for confirmation. – huynhjl Oct 14 '11 at 14:44
  • Thought it might be a typo. Thanks for clarification! – overthink Oct 14 '11 at 14:56
1

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.
overthink
  • 23,985
  • 4
  • 69
  • 69