Java Reference
In-Depth Information
amount of time before it is killed forcibly. Interacting with a user should
be done before shutdown, not during it.
23.3.2. The Shutdown Sequence
The shutdown sequence is initiated when the last user thread termin-
ates, the Runtime.exit method is invoked, or the external environment
signals the virtual machine to shutdown. When shutdown is initiated all
the shutdown hook threads are started and allowed to run to comple-
tion. If any of these threads fails to terminate the shutdown sequence
will not complete. If shutdown was initiated internally the virtual ma-
chine will not terminate. If shutdown was signaled from the external en-
vironment then failure to shutdown may result in a forced termination
of the virtual machine.
If a shutdown hook thread incurs an uncaught exception, no special ac-
tion is taken; it is handled like any other uncaught exception in a thread.
In particular, it does not cause the shutdown process to abort.
When the last shutdown hook thread has terminated, the halt method
will be invoked. It is halt that actually causes the virtual machine to
cease running. The halt method also takes an integer status as an ar-
gument whose meaning is the same as that of exit : Zero indicates suc-
cessful execution of the entire virtual machine. A shutdown hook can
invoke halt to end the shutdown phase. Invoking halt directlyeither be-
fore or during the shutdown phaseis dangerous since it prevents uncom-
pleted hooks from doing their cleanup. You will probably never be in a
situation where invoking halt is correct.
If exit is called while shutdown is in progress the call to exit will block
indefinitely. The effect of this on the shutdown sequence is not specified,
so don't do this.
In rare circumstances, the virtual machine will abort rather than perform
an orderly shutdown. This can happen, for example, if an internal error
is detected in the virtual machine that prevents an orderly shutdown-
such as errant native code overwriting system data structures. However,
the environment hosting the virtual machine can also force the virtual
 
Search WWH ::




Custom Search