com.mindprod.common11
Class StoppableThread

java.lang.Object
  extended by java.lang.Thread
      extended by com.mindprod.common11.StoppableThread
All Implemented Interfaces:
java.lang.Runnable

public final class StoppableThread
extends java.lang.Thread

A thread you can stop gently and safely.

Remember to nullify references to the thread after you are finished. Threads are big. You can't restart it! Required for multi-cpu cache synchronization.

Since:
2002-08-01
Version:
1.2 2006-03-04
Author:
Roedy Green, Canadian Mind Products

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
StoppableThread(java.lang.Runnable r)
          Constructor.
 
Method Summary
 void gentleStop(boolean interrupt, long timeout)
          Stop this thread gracefully.
 void start()
          Start this thread executing its run method on a separate thread.
 boolean stopping()
          this.run should call stopping() at convenient invervals to see if it has been requested to stop.
 
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, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StoppableThread

public StoppableThread(java.lang.Runnable r)
Constructor.

Parameters:
r - class that has a run method
Method Detail

gentleStop

public void gentleStop(boolean interrupt,
                       long timeout)
Stop this thread gracefully. If the thread is already stopped, does nothing. For this to work, this.run must exit by checking stopping() at convenient intervals and returning if it is true.

Parameters:
interrupt - true if this thread should be interrupted from sleep or from doing an I/O (which might close the channel), before stopping it.
timeout - How long in milliseconds to wait for the thread to die before giving up. 0 means wait forever. -1 means don't wait at all.

start

public void start()
Start this thread executing its run method on a separate thread. You may only call start once. After the thread dies it cannot be restarted.

Overrides:
start in class java.lang.Thread
Throws:
java.lang.IllegalThreadStateException - if this thread is already started.

stopping

public final boolean stopping()
this.run should call stopping() at convenient invervals to see if it has been requested to stop. If true, it should finish up quickly and return. Will have to be called via while ( ! ((StoppableThead)Thread.currentThread()).stopping() )

Returns:
true if run should exit soon.