com.pelzer.util
Class KillableThread
java.lang.Object
java.lang.Thread
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();
|
Field Summary |
boolean |
die
Implementing classes should watch this value, and terminate gracefully if it ever becomes true. |
| 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 |
die
public volatile boolean die
- Implementing classes should watch this value, and terminate gracefully if it ever becomes true.
KillableThread
public KillableThread()
Copyright © 2012. All Rights Reserved.