com.pelzer.util
Class KillableThread

java.lang.Object
  extended by java.lang.Thread
      extended by com.pelzer.util.KillableThread
All Implemented Interfaces:
Runnable

public abstract class KillableThread
extends Thread

There is no built-in mechanism for stopping threads in Java, so this class is a simple fix for the issue. Any thread that you need another thread to be able to signal to shut down should have a run() method that looks something like this:

  run(){
        while(!die){
                doSomething();
                sleep(...);
        }
  }
Another thread can then request the killable thread shut down gracefully by doing the following:
  killableThread.die = true;
  killableThread.waitFor();
  


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 boolean die
          Implementing classes should watch this value, and terminate gracefully if it ever becomes true.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
KillableThread()
           
 
Method Summary
 
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, run, 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

die

public volatile boolean die
Implementing classes should watch this value, and terminate gracefully if it ever becomes true.

Constructor Detail

KillableThread

public KillableThread()


Copyright © 2012. All Rights Reserved.