us.ihmc.utilities.thread
Class InterruptableWorker

java.lang.Object
  extended by us.ihmc.utilities.thread.InterruptableWorker
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
DebugInterruptableWorker

public abstract class InterruptableWorker
extends java.lang.Object
implements java.lang.Runnable

Title:

InterruptableThread

Description:

This class is designed to be a pattern for threads that need to be interruptable (usually time limited). It also manages the tracking the state during operations.


Nested Class Summary
static class InterruptableWorker.STATE
           
 
Constructor Summary
InterruptableWorker()
           
 
Method Summary
abstract  InterruptableWorker.STATE doWork()
          This method needs to be overridden and should perform the actual work It should periodically chech the isInterrupted method to know if it should stop.
abstract  java.lang.Object getCurrentResult()
          This method needs to be overridden and should be used by the application to get the current result while waiting as well as get the final result once finished
 InterruptableWorker.STATE getCurrentState()
          This method returns the current state
 boolean isInterrupted()
          This method should be used by the InterruptableWorker to check if it has been interrupted It returns true when stop has been called and stays true until reset
 boolean isWorkingOrPendingInterruption()
           
 void reset()
          This method should be overridden, but should call this method as the first step.
 void run()
           
 void setCurrentState(InterruptableWorker.STATE state)
           
 void startWorkOnANewThread()
          This is the normal thread run method called on start It tells the worker to start and tracks the resulting state The worker should return the appropriate finish state: SUCCESSFULLY_COMPLETED UNSUCCESSFULLY_COMPLETED INTERRUPTED
 void startWorkOnThisThread()
           
 void stopWork()
          This method is used to signal the worker to stop and then wait until worker finishes to return
 void waitForResult(long timeout)
          This method should be used by the application to block until work is finished It will block until the timeout and interrupt the worker if not finished in time It does not guarentee the timeout period because it will depend on how long it takes the worker to finish after being told to stop.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InterruptableWorker

public InterruptableWorker()
Method Detail

doWork

public abstract InterruptableWorker.STATE doWork()
This method needs to be overridden and should perform the actual work It should periodically chech the isInterrupted method to know if it should stop. It should return one of the three acceptable finish states: SUCCESSFULLY_COMPLETED UNSUCCESSFULLY_COMPLETED INTERRUPTED

Returns:
STATE

getCurrentResult

public abstract java.lang.Object getCurrentResult()
This method needs to be overridden and should be used by the application to get the current result while waiting as well as get the final result once finished

Returns:
Object

reset

public void reset()
This method should be overridden, but should call this method as the first step.


getCurrentState

public InterruptableWorker.STATE getCurrentState()
This method returns the current state

Returns:
STATE

startWorkOnANewThread

public void startWorkOnANewThread()
This is the normal thread run method called on start It tells the worker to start and tracks the resulting state The worker should return the appropriate finish state: SUCCESSFULLY_COMPLETED UNSUCCESSFULLY_COMPLETED INTERRUPTED


startWorkOnThisThread

public void startWorkOnThisThread()

run

public void run()
Specified by:
run in interface java.lang.Runnable

setCurrentState

public void setCurrentState(InterruptableWorker.STATE state)

stopWork

public void stopWork()
This method is used to signal the worker to stop and then wait until worker finishes to return


waitForResult

public void waitForResult(long timeout)
This method should be used by the application to block until work is finished It will block until the timeout and interrupt the worker if not finished in time It does not guarentee the timeout period because it will depend on how long it takes the worker to finish after being told to stop.

Parameters:
timeout - long in milliseconds

isInterrupted

public boolean isInterrupted()
This method should be used by the InterruptableWorker to check if it has been interrupted It returns true when stop has been called and stays true until reset

Returns:
boolean

isWorkingOrPendingInterruption

public boolean isWorkingOrPendingInterruption()