statalign.base.thread
Class StoppableThread

java.lang.Object
  extended by java.lang.Thread
      extended by statalign.base.thread.StoppableThread
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
MainThread

public class StoppableThread
extends java.lang.Thread

Objects of this class can be used in place of a Thread object if it is intended that they can be stopped or suspended "softly" by an external thread. This means that if an external thread requests termination of this thread (by calling stopSoft()), execution will continue up to the first "stopping point", i.e. the first call of stoppable() from within this thread. At this point, a StoppedException will be thrown, and the StoppableThread will be given the chance to catch it at an arbitrary point and do up before termination. In every other aspect, a StoppableThread is like a Thread, e.g. you should override run() and place your "softly stoppable" code there. But you must declare your run() method synchronized for most of the above to work properly. The easiest way to define "stopping points" and "pausing points" in your own classes is to use Stoppable as the ancestor and call its stoppable() and pausable() methods from your methods.

Author:
novak

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()
           
 
Method Summary
 void pauseNoWait()
          Can be called by an external thread to request suspension of this thread.
 void pauseToggleSoft()
          Calls suspendSoft() or resumeSoft(), depending on the current state of this thread.
 void resumeSoft()
          Resumes execution of this thread if it has been previously paused by pauseSoft().
 void stopNoWait()
          Similar to stopSoft() but non-blocking, and also will not wake up this thread if it is suspended.
 void stopSoft()
          Can be called by an external thread to request termination of this thread.
 void suspendSoft()
          Can be called by an external thread to request suspension of this thread.
 
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, run, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StoppableThread

public StoppableThread()
Method Detail

suspendSoft

public void suspendSoft()
Can be called by an external thread to request suspension of this thread. The method will not return until execution of this thread reaches a "pausing" or "stopping point", i.e. until pausable() or stoppable() is called from within this thread. At that point, this thread will be suspended until resumeSoft() or stopSoft() is called by the external thread. Compare deprecated "hard" suspend: Thread.suspend()


pauseToggleSoft

public void pauseToggleSoft()
Calls suspendSoft() or resumeSoft(), depending on the current state of this thread.


pauseNoWait

public void pauseNoWait()
Can be called by an external thread to request suspension of this thread. Unlike pauseSoft(), this method is non-blocking, but otherwise has the same effect.


resumeSoft

public void resumeSoft()
Resumes execution of this thread if it has been previously paused by pauseSoft(). To be called by an external thread. Non-blocking, even if pauseNoWait() was used and this thread has not yet reached a "pausing point". Compare deprecated "hard" resume: Thread.resume()


stopSoft

public void stopSoft()
Can be called by an external thread to request termination of this thread. This method will not return until execution of this thread reaches a "stopping point", i.e. until stoppable() is called from within this thread. Can also be used if this thread has been suspended by suspendSoft(). Compare deprecated "hard" stop: Thread.stop()


stopNoWait

public void stopNoWait()
Similar to stopSoft() but non-blocking, and also will not wake up this thread if it is suspended. In that case, use (non-blocking) resumeSoft() first.