When I press ctrl-c in console in what sequence are application threads stopped and shutdown hooks called?
ctrl c is used to kill a process. It terminates your program.
Ctrl+D also causes the terminal to make buffered input available, or, if nothing is buffered, to terminate the input. Asking for a program to do something on Ctrl+D is essentially asking it to do something when it reads all available input (and more may become available later).
According to the javadocs, the registered shutdown hooks are called in an unspecified order when the JVM starts shutting down; e.g. in response to a CTRL-C.
Application threads are not "stopped" in any well defined way. Indeed, they could continue running up right until the process exits.
If you want your threads to be shut down in an orderly fashion, you need to do something in a shutdown hook to cause this to happen. For example, a shutdown hook could call Thread.interrupt()
to tell worker threads to stop what they are doing ... and call join()
to make sure that it has happened.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With