Sunday, 30 November 2008

The Idiosyncracies of the Java VM Shutdown Sequence

JVM shutdown is in two parts.

  1. All registered shutdown hooks
  2. All uninvoked finalizers are run

Runtime.getRuntime().addShutdownHook(Thread) registers a new VM shutdown hook.

A shutdown hook is simply an "initialized but unstarted thread". When VM shuts down, it starts all shutdown hooks and runs them concurrently. Then it will run all uninvoked finalizers(assuming finalization-on-exit has been enabled).

Once shutdown has been initiated it can only be stopped by invoking the halt method, to forcibly terminate the JVM. halt has one argument which is status code. Any non-zero value indicates abnormal termination.

Read more in the Runtime (singleton) class documentation below:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html

No comments: