tylerhayes.tools
Class TerminationInterceptor

java.lang.Object
  extended by java.lang.Thread
      extended by tylerhayes.tools.TerminationInterceptor
All Implemented Interfaces:
java.lang.Runnable

public class TerminationInterceptor
extends java.lang.Thread

This Class, along with the GracefulShutdown Interface, comprise a simple signal handling mechanism for catching a kill signal--more specifically, when a user hits CTRL-C when running a program through a command-line (won't work when clicking the "Termination" button in Eclipse).

In order to have a kill signal handler, the call to Runtime.getRuntime().addShutdownHook(Thread) must be given a Thread object. This object extends Thread and is used as such. It is only run upon the termination of the program, thus the run() method's simple call to the shutDown() method of its private GracefulShutdown Interface instance.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TerminationInterceptor(GracefulShutdown gracefulShutdown)
          Creates a new TerminationInterceptor object with the given GracefulShutdown Interface instance.
 
Method Summary
 void run()
          Calls the object's GracefulShutdown instance's shutDown() method.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, 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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TerminationInterceptor

public TerminationInterceptor(GracefulShutdown gracefulShutdown)
Creates a new TerminationInterceptor object with the given GracefulShutdown Interface instance.

Parameters:
gracefulShutdown - Usually an instance of the class that contains the main method of the program (which has to implement the GracefulShutdown Interface, of course).
Method Detail

run

public void run()
Calls the object's GracefulShutdown instance's shutDown() method. This is run when the program terminates--either in code, or by the user hitting CTRL-C when running the program from the command-line.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread