Java Reference
In-Depth Information
23.3. Shutdown
Normally, an execution of a virtual machine terminates when the last
user thread terminates. A
Runtime
can also be shut down explicitly with its
exit
method, passing an integer status code that can be communicated
to the environment executing the virtual machinezero to indicate suc-
cessful completion of a task and non-zero to indicate failure. This meth-
od abruptly terminates all threads in the runtime system no matter what
their state. They are not interrupted or even stopped. They simply cease
to exist as the virtual machine itself stops runningno
finally
clauses are
executed.
In either case, when
exit
is invoked or the last user thread terminates,
the shutdown sequence is initiated. The virtual machine can also be shut
down externally, such as by a user interrupting the virtual machine from
a keyboard (on many systems by typing control-C) or when the user logs
out or the computer is shut down.
All the methods related to shutting down the runtime system are security
checked and throw
SecurityException
if you don't have required permis-
sions.
23.3.1. Shutdown Hooks
An application can register a
shutdown hook
with the runtime system.
Shutdown hooks are threads that represent actions that should be taken
before the virtual machine exits. Hooks typically clean up external re-
sources such as files and network connections.
public void
addShutdownHook(Thread hook)
Registers a new virtual-machine shutdown hook. If
hook
has
already been registered or has already been started an
Illeg-
alArgumentException
is thrown.
public boolean
removeShutdownHook(Thread hook)