The scaladoc for sys.addShutdownHook
says shutdown hooks are NOT guaranteed to be run
. Now this is entirely reasonable, as the JVM can hardly run shutdown hooks if you send the JVM a SIGKILL, or whatever the Windows equivalent is.
However shutdown hooks added with sys.addShutdownHook
never seem to run, although those run with Runtime.getRuntime.addShutdownHook
do.
A test -
scala> val t = new Thread { override def run = println("hi!") }
t: java.lang.Thread = Thread[Thread-4,5,main]
scala> Runtime.getRuntime.addShutdownHook(t)
scala> hi!
george@george-MacBook:~$ scala
(Skipped the startup message)
scala> val t = new Thread { override def run = println("hi!") }
t: java.lang.Thread = Thread[Thread-4,5,main]
scala> sys.addShutdownHook(t.run _)
res0: scala.sys.ShutdownHookThread = Thread[shutdownHook1,5,main]
scala> george@george-MacBook:~$
The documentation says "The hook is automatically registered: the returned value can be ignored" so it's not that we're supposed to add the thread returned by sys.addShutdownHook
(and at any rate that causes "IllegalArgumentException: Hook previously registered" to be thrown).
Also, calling run on thread returned by addShutdownHook doesn't seem to do anything, which is suspicious.