java - Why does ShutdownHookThread 'setDaemon true' -
i needed add shutdown hook scala app have, , discovered scala provides helper called shutdownhookthread. in source noticed it sets new thread 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 done? seems me you'd want opposite in shutdown hook thread (i.e. make sure thread exits before shutting down jvm). or daemon/not-daemon not relevant shutdown hooks?
on jvm, in general non-daemon thread prevent jvm terminating. once there no longer non-daemon threads, jvm gracefully terminate initiating shutdown. see addshutdownhook javadoc more info.
once shutdown has been initiated, i'm not sure daemon status matters. shutdown hook threads aren't started until shutdown has been initiated. in case t setdaemon true
may unnecessary, won't hurt either.
so in short "daemon" semantic differs unix (where in unix land denotes thread keeps running).
Comments
Post a Comment