This library extends java.lang.Thread class to provide clean and reliable thread terminating capability.
The usual method prescribed by many, to simulate thread kill, is to use a control variable which thread needs to keep checking. When thread needs to be killed, it is sent an interrupt signal and the interrupt handler sets this variable to some value to indicate that thread needs to die. This works only when thread can come out of a blocking operation like database call or accepting a socket or finishing reading a file.
Note that the default interrupt function available on Thread works only when the thread is in sleep/wait state.
There is no way to interrupt a busy thread
The new approach uses control resource and thread shutdown hook concepts. The control resource could be an open file or database connection or anything else which is crucial to thread's normal life cycle. If you close this resource, thread dies. The programmer adds the hooks to close these control resources. Here's what a hook looks like ...
package open.ejaz.tthread;
/**
TerminateThread class extends Thread class. It keeps a stack of terminate hooks (per instance of TerminateThread) which are added by a programmer at run time. Thus when developer invokes interrupt on TerminativeThread's instance, it checks all the available hooks and invokes each one them in the order in which they were added