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?
Answering my own question here.
Two parts:
ShutdownHookThread
make its new threads daemon=true?Answers:
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.