Using RunTime ShutdownHook
A shutdown hook is simply an instance of a subclass of the Thread class. Creating a shutdown hook is simple:

1. Write a class extending the Thread class.
2. Provide the implementation of your class’ run method. This method is the code that needs to be run when the application is shut down, either normally or abruptly.
3. In your application, instantiate your shutdown hook class.
4. Register the shutdown hook with the current runtime’s addShutdownHook method.

Surviving Abrupt Shutdown
JBoss 4.0.2 org.jboss.system.Server.shutdown()
Tomcat 4.1.34 shutdown()

Using a daemon thread with ShutdownHook
For example, JBoss uses shutdownHook.setDaemon(true) or do this (Java Daemon).