THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
public booleanremoveShutdownHook(Thread hook)

Unregisters a previously registered virtual machine shutdown hook. Returns
TRue if hook was registered and has been unregistered. Returns false if
hook was not previously registered.

You cannot add or remove shutdown hooks after shutdown has commenced; you will get
IllegalStateException if you try.


When shutdown is initiated, the virtual machine will invoke the start method on all shutdown hook
THRead objects. You cannot rely on any orderingshutdown hook threads may be executed before, after, or at
the same time as any other shutdown hook thread depending on thread scheduling.


You must be careful about this lack of ordering. Suppose, for example, you were writing a class that stored
state in a database. You might register a shutdown hook that closed the database. However, a program using
your class might want to register its own shutdown hook that writes some final state information through your
class to the database. If your shutdown hook is run first (closing the database), the program's shutdown hook
would fail in writing its final state. You can improve this situation by writing your class to reopen the
database when needed, although this might add complexity to your class and possibly even some bad race
conditions. Or you might design your class so that it doesn't need any shutdown hooks.


It is also important to realize that the shutdown hook threads will execute concurrently with other threads in
the system. If shutdown was initiated because the last user thread terminated, then the shutdown hook threads
will execute concurrently with any daemon threads in the system. If shutdown was initiated by a call to exit,
then the shutdown hook threads will execute concurrently with both daemon and user threads. Your shutdown
hook threads must be carefully written to ensure correct synchronization while avoiding potential deadlocks.


Your shutdown hooks should execute quickly. When users interrupt a program, for example, they expect the
program to terminate quickly. And when a virtual machine is terminated because a user logs out or the
computer is shut down, the virtual machine may be allowed only a small 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 terminates, 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 completion. If any of these threads fails to
terminate the shutdown sequence will not complete. If shutdown was initiated internally the virtual machine
will not terminate. If shutdown was signaled from the external environment 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 action 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
argument whose meaning is the same as that of exit: Zero indicates successful execution of the entire virtual
machine. A shutdown hook can invoke halt to end the shutdown phase. Invoking halt directlyeither before
or during the shutdown phaseis dangerous since it prevents uncompleted 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.

Free download pdf