edu.rice.cs.plt.swing
Class SwingWorker<R,I>

java.lang.Object
  extended by edu.rice.cs.plt.concurrent.TaskController<R>
      extended by edu.rice.cs.plt.concurrent.IncrementalTaskController<I,R>
          extended by edu.rice.cs.plt.swing.SwingWorker<R,I>
All Implemented Interfaces:
ResolvingThunk<R>, Thunk<R>, Future<R>

public abstract class SwingWorker<R,I>
extends IncrementalTaskController<I,R>

A utility class providing the core functionality of javax.swing.SwingWorker (first available in Java 6), in addition to supporting the IncrementalTaskController interface. Allows a task to be separated into two parts: a working portion that calculates a value (or sequence of intermediate values) in the background, and a GUI portion that executes in the Swing event thread when the working portion is complete. Implementations should define doInBackground() and, optionally, process(java.util.List) and done(). The doInBackground() implementation may call publish(I...) and authorizeContinue(); other protected methods should generally be ignored as implementation details.

Implementations should be able to migrate seamlessly to the Java 6 API version by simply changing the parent class, as long as they stick to methods that are defined in both classes. This version doesn't support PropertyChangeListeners, a progress property, the Runnable interface, a getState() method (although status() provides similar information), or an isCancelled() method that can be polled by doInBackground() (isCancelled() is defined, but its result is never true before doInBackground() returns; implementations should instead handle cancellation by checking for an interrupt). There is also (currently) no thread pooling — each worker runs in a new thread. This version adds the methods provided by IncrementalTaskController, as well as authorizeContinue(), which supports the implementation of IncrementalTaskController.pause().


Nested Class Summary
 
Nested classes/interfaces inherited from class edu.rice.cs.plt.concurrent.IncrementalTaskController
IncrementalTaskController.CanceledPausingState, IncrementalTaskController.FreshPausingState, IncrementalTaskController.PausedStartingState, IncrementalTaskController.PausedState, IncrementalTaskController.PausingState, IncrementalTaskController.StartedPausingState
 
Nested classes/interfaces inherited from class edu.rice.cs.plt.concurrent.TaskController
TaskController.CanceledStartingState, TaskController.CanceledState, TaskController.CancelingState, TaskController.CleanlyFinishedState, TaskController.ComputingState, TaskController.ExecutionExceptionState, TaskController.FinishedState, TaskController.FreshStartingState, TaskController.FreshState, TaskController.InternalExceptionState, TaskController.RunningState, TaskController.StartingState, TaskController.State, TaskController.Status, TaskController.WaitingState
 
Field Summary
 
Fields inherited from class edu.rice.cs.plt.concurrent.TaskController
state
 
Constructor Summary
SwingWorker()
           
 
Method Summary
protected  void authorizeContinue()
          Called by doInBackground() to ensure that the task has not been paused or canceled.
protected abstract  R doInBackground()
          Work to be performed in a worker thread.
protected  void done()
          Action to be performed in the event thread when work has completed.
protected  void doPause()
          Pause computation and call IncrementalTaskController.paused().
protected  void doResume()
          Resume computation (after a pause) and call TaskController.started().
protected  void doStart()
          Begin computation and call TaskController.started().
protected  void doStop()
          Terminate computation and call TaskController.stopped().
 void execute()
           
protected  void process(List<I> chunks)
          Action to be performed in the event thread when intermediate results are available.
protected  void publish(I... chunks)
          Called by doInBackground() when intermediate results are available.
 
Methods inherited from class edu.rice.cs.plt.concurrent.IncrementalTaskController
intermediateListeners, intermediateQueue, pause, paused, stepped, steps
 
Methods inherited from class edu.rice.cs.plt.concurrent.TaskController
attemptGet, attemptGet, cancel, cancel, discard, finishedCleanly, finishedWithImplementationException, finishedWithTaskException, finishListeners, get, get, get, hasValue, isCanceled, isCancelled, isDone, isResolved, runningState, start, started, status, stopped, value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SwingWorker

public SwingWorker()
Method Detail

execute

public final void execute()

doInBackground

protected abstract R doInBackground()
                             throws Exception
Work to be performed in a worker thread.

Throws:
Exception

process

protected void process(List<I> chunks)
Action to be performed in the event thread when intermediate results are available.


done

protected void done()
Action to be performed in the event thread when work has completed.


publish

protected final void publish(I... chunks)
Called by doInBackground() when intermediate results are available.


authorizeContinue

protected void authorizeContinue()
                          throws InterruptedException

Called by doInBackground() to ensure that the task has not been paused or canceled. If paused, this method blocks until the task is restarted. If canceled, throws an InterruptedException.

Tasks that wish to maintain migration compatibility with javax.swing.SwingWorker cannot call this method or support pausing. Instead, they should simply respond to an interrupt when canceled (checking, for example, via Thread.interrupted()).

Throws:
InterruptedException

doStart

protected final void doStart()
Description copied from class: TaskController
Begin computation and call TaskController.started(). If starting does not occur immediately (for example, blocking occurs first), the started() call may occur in a different thread.

Specified by:
doStart in class TaskController<R>

doPause

protected final void doPause()
Description copied from class: IncrementalTaskController
Pause computation and call IncrementalTaskController.paused(). If pausing does not occur immediately, the paused() call may occur in a different thread. Will only be called after started() has been invoked. When execution should resume again, doStart() will be invoked (but only after paused() has been called). In order to support responsive canceling, a call to doStop() may occur concurrently, or before paused() is called.

Specified by:
doPause in class IncrementalTaskController<I,R>

doResume

protected final void doResume()
Description copied from class: IncrementalTaskController
Resume computation (after a pause) and call TaskController.started(). If starting does not occur immediately (for example, blocking occurs first), the started() call may occur in a different thread.

Specified by:
doResume in class IncrementalTaskController<I,R>

doStop

protected final void doStop()
Description copied from class: TaskController
Terminate computation and call TaskController.stopped(). Never called before started() has been invoked. If termination does not occur immediately (for example, blocking occurs first), the stopped() call may occur in a different thread.

Specified by:
doStop in class TaskController<R>