util
Class CleanShutdown

java.lang.Object
  extended by java.lang.Thread
      extended by util.CleanShutdown
All Implemented Interfaces:
Runnable

public class CleanShutdown
extends Thread

A shutdown-hook that allows ExitCleaning and CancelRunnable instances to be registered to it. On shutdown, all registered ExitCleaning are brought into a safe state by invoking their ExitCleaning.exit() method. All CancelRunnable instances are canceled using their CancelRunnable.cancel() method. To provide at least some order, ExitCleaning and CancelRunnable instances may be added to this class using an additional priority parameter. Those instances with a low priority are executed first. If no priority is given, DEFPRIORITY is asumed.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
private static List<Object> avoid
          A look-up list to avoid double-adding of ExitCleaning objects.
private static TreeMap<Integer,List<Object>> clean
          A map of ExitCleaning instances to be cleaned on shutdown.
private static CleanShutdown CLEANSE
          A shutdown-hook collecting necessary pre-exit jobs.
static int DEFPRIORITY
          The default priority.
private static boolean HAVECLEANSED
          True, if the CleanShutdown hook was added.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
private CleanShutdown()
          Constructs a new shutdown-hook.
 
Method Summary
static void addShutdown(Object cleanup)
          Adds an exit-cleaning instance to this shutdown hook using the default priority DEFPRIORITY.
static void addShutdown(Object cleanup, int priority)
          Adds an exit-cleaning instance to this shutdown hook.
 void removeShutdown(Object remove)
          Removes a previously registered ExitCleaning or CancelRunnable instance from this shutdown hook.
 void run()
          Performs a ordered shutdown of all registered ExitCleaning and CancelRunnable instances.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFPRIORITY

public static final int DEFPRIORITY
The default priority.

See Also:
Constant Field Values

clean

private static TreeMap<Integer,List<Object>> clean
A map of ExitCleaning instances to be cleaned on shutdown.


avoid

private static List<Object> avoid
A look-up list to avoid double-adding of ExitCleaning objects.


CLEANSE

private static CleanShutdown CLEANSE
A shutdown-hook collecting necessary pre-exit jobs.


HAVECLEANSED

private static boolean HAVECLEANSED
True, if the CleanShutdown hook was added.

Constructor Detail

CleanShutdown

private CleanShutdown()
Constructs a new shutdown-hook. Prior to shutdown, ExitCleaning instances may be registered and deregistered from the hook using the addShutdown(java.lang.Object) and removeShutdown(java.lang.Object) methods.

Method Detail

addShutdown

public static void addShutdown(Object cleanup)
Adds an exit-cleaning instance to this shutdown hook using the default priority DEFPRIORITY.


addShutdown

public static void addShutdown(Object cleanup,
                               int priority)
Adds an exit-cleaning instance to this shutdown hook. Only added if not already present and instance of exit cleaning. The priority is used in a sorted map as a key pointing to a list of equal-priority ExitCleaning and CancelRunnable instances. avoid is used to block multiple insertion.


removeShutdown

public void removeShutdown(Object remove)
Removes a previously registered ExitCleaning or CancelRunnable instance from this shutdown hook. If present in the avoid list, it is removed also from the priority map by roaming through all values.


run

public void run()
Performs a ordered shutdown of all registered ExitCleaning and CancelRunnable instances. First, all CancelRunnable.cancel() methods are invoked. Then, depending on their priority, the rgistered objects are finalized in a way that for ExitCleaning instances their ExitCleaning.exit() method is invoked and CancelRunnable instances are joined to for CancelRunnable.maxTimeToCancel(). Note that this clean-up is done purely seqeuntial, which suggest that ExitCleaning instances should be cleaned up fast and that CancelRunnable instances should be finalized with low priority.
If a class implements both interface, the CancelRunnable is finalizing is done before the exit method.

Specified by:
run in interface Runnable
Overrides:
run in class Thread