edu.hawaii.ics.yucheng
Class BackgroundWorker<TResult,TProgress>

java.lang.Object
  extended by edu.hawaii.ics.yucheng.BackgroundWorker<TResult,TProgress>
Direct Known Subclasses:
SpanningTreeApplet.Worker

abstract class BackgroundWorker<TResult,TProgress>
extends java.lang.Object

An abstract class that provides the base functionality for background worker threads. This class works with the AWT event queue to transfer messages to and from the GUI and worker thread. This class is generic and supports any kind of progress message or result.


Nested Class Summary
(package private)  class BackgroundWorker.Delegate
          A delegate class used to transfer messages between the GUI thread and the background worker thread.
 
Field Summary
private static int ACTION_ERROR
           
private static int ACTION_PROGRESS
           
private static int ACTION_RESULT
           
private static int ACTION_START
           
private  boolean myCancelFlag
           
private  java.lang.Thread myThread
           
 
Constructor Summary
BackgroundWorker()
           
 
Method Summary
 void cancel()
          Requests that the background worker should cancel.
 boolean isCancelled()
          This method returns true if the GUI thread has requested that the background worker thread should cancel gracefully.
 boolean isRunning()
          Returns true if the background worker is running and false otherwise.
 void join()
          Joins the current thread with the worker thread.
(package private)  void processDelegate(BackgroundWorker.Delegate delegate)
          Processes the delegate action.
protected  void processError(java.lang.Exception exception)
          When overridden by a derived class, this method processes an error thrown from the background worker thread.
protected  void processProgress(TProgress progress)
          When overridden by a derived class, this method processes a progress message sent by the background worker thread.
protected  void processResult(TResult result)
          When overridden by a derived class, this method processes a result returned from the background worker thread.
protected abstract  TResult run()
          This method runs from a background thread after the start() method has been called.
protected  void sendProgress(TProgress progress)
          Sends a progress message from the background worker thread to the GUI thread.
 void start()
          This method starts the background worker thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACTION_ERROR

private static final int ACTION_ERROR
See Also:
Constant Field Values

ACTION_PROGRESS

private static final int ACTION_PROGRESS
See Also:
Constant Field Values

ACTION_RESULT

private static final int ACTION_RESULT
See Also:
Constant Field Values

ACTION_START

private static final int ACTION_START
See Also:
Constant Field Values

myCancelFlag

private boolean myCancelFlag

myThread

private java.lang.Thread myThread
Constructor Detail

BackgroundWorker

BackgroundWorker()
Method Detail

cancel

public void cancel()
Requests that the background worker should cancel. If the background worker is not running, then this method has no effect.


isCancelled

public boolean isCancelled()
This method returns true if the GUI thread has requested that the background worker thread should cancel gracefully.

Returns:
True indicates the thread should cancel and false otherwise.

isRunning

public boolean isRunning()
Returns true if the background worker is running and false otherwise.

Returns:
True if the background worker is running and false otherwise.

join

public void join()
          throws java.lang.InterruptedException
Joins the current thread with the worker thread. This method returns immediately if the background thread is not running.

Throws:
java.lang.InterruptedException - Thrown if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

processDelegate

void processDelegate(BackgroundWorker.Delegate delegate)
Processes the delegate action. This executes in either the GUI thread or the background worker thread depending on the type of action that should be performed.

Parameters:
delegate - The delegate that is running.

processError

protected void processError(java.lang.Exception exception)
When overridden by a derived class, this method processes an error thrown from the background worker thread. The default implementation does nothing but displays the error message to standard error output.

Parameters:
exception - The error thrown by the background worker thread.

processProgress

protected void processProgress(TProgress progress)
When overridden by a derived class, this method processes a progress message sent by the background worker thread. The default implementation does nothing.

Parameters:
progress - The progress data.

processResult

protected void processResult(TResult result)
When overridden by a derived class, this method processes a result returned from the background worker thread. The default implementation does nothing.

Parameters:
result - The result from the background worker thread.

run

protected abstract TResult run()
                        throws java.lang.Exception
This method runs from a background thread after the start() method has been called. The value returned by this method is later processed in the processResult() method. If the implementation wishes to send progress messages to the GUI thread, it should call the sendProgress() method, which will trigger a progress message to arrive in the processProgress() method.

Returns:
The result from the background worker thread.
Throws:
java.lang.Exception - Thrown if any kind of exception occurs during execution.

sendProgress

protected void sendProgress(TProgress progress)
Sends a progress message from the background worker thread to the GUI thread. The message arrived via the processProgress() method. This method does not wait for the message to arrive and returns immediately.

Parameters:
progress - The progress message.

start

public void start()
This method starts the background worker thread. This method should not be called unless the thread has not already started.

Throws:
java.lang.IllegalStateException - Thrown if the thread has already started.