FutureTalker V1.3
by Matthew Ford
2005/04/11

au.com.forward.futureTalker
Class FutureTalker<V>

java.lang.Object
  extended by java.util.concurrent.FutureTask<V>
      extended by au.com.forward.futureTalker.FutureTalker<V>
All Implemented Interfaces:
java.lang.Runnable, java.util.concurrent.Future<V>

public class FutureTalker<V>
extends java.util.concurrent.FutureTask<V>

This class allows you to add listeners to your FutureTask.
These listeners are called when task completes, throws an error or is cancelled.

Your listeners need to implement IFutureListener. Typicall usage is

      FutureTalker talker = new FutureTalker(callable));
      talker.addListener(listener);
      try {
        threadPool.execute(talker);
      } catch (RejectedExecutionException rex) {
        talker.rejected(rex);  // failed to execute set task to error and notifies listeners
      }
 
where callable implements Callable and
threadPool implements Executor


Field Summary
protected  java.util.List<IFutureListener<V>> listenerList
          The list of listeners for this task
 
Constructor Summary
FutureTalker(java.util.concurrent.Callable<V> callable)
          Creates a FutureTalker that will upon running, execute the given Callable.
 
Method Summary
 void addListener(IFutureListener<V> listener)
          Adds a listener to this task.
protected  void done()
          fire the listeners when task terminates or is cancelled
protected  void fireListeners()
          Fires currently registered listeners and removes them.
 java.util.concurrent.Callable<V> getCallable()
          Returns the Callable that this task executes
static void ifInterruptedStop()
          Checks in the interrupt flag is set for the thread running this task and throws an InterruptedException if it is.
 void rejected(java.util.concurrent.RejectedExecutionException r)
          Causes this future to report an ExecutionException with the given throwable as its cause, unless this Future has already been set or has been cancelled.
 void removeListener(IFutureListener<V> listener)
          Remove a listener from this task, if it is found.
static java.util.List<java.lang.Runnable> shutdownNow(java.util.concurrent.ThreadPoolExecutor threadPool)
          Shut down a ThreadPoolExecutor and reject or cancel unstarted pending tasks.
 
Methods inherited from class java.util.concurrent.FutureTask
cancel, get, get, isCancelled, isDone, run, runAndReset, set, setException
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listenerList

protected java.util.List<IFutureListener<V>> listenerList
The list of listeners for this task

Constructor Detail

FutureTalker

public FutureTalker(java.util.concurrent.Callable<V> callable)
Creates a FutureTalker that will upon running, execute the given Callable. use
FutureTalker(Executors.callable(runnable, result)) for runnable

Parameters:
callable - the callable task
Throws:
java.lang.NullPointerException - if callable is null
Method Detail

ifInterruptedStop

public static void ifInterruptedStop()
                              throws java.lang.InterruptedException
Checks in the interrupt flag is set for the thread running this task and throws an InterruptedException if it is.

In order to stop the task, this InterruptedException must propergate out of the Callable.call() method.

If this check is done in an internal method which is not defined to throw exceptions you will have to catch the InterruptedException and wrap it in a RuntimeException and re-throw it.

Throws:
java.lang.InterruptedException - Thrown if the interrupt flag is set for the thread running this method.

shutdownNow

public static java.util.List<java.lang.Runnable> shutdownNow(java.util.concurrent.ThreadPoolExecutor threadPool)
Shut down a ThreadPoolExecutor and reject or cancel unstarted pending tasks.
Note: Tasks that have already been started will be interrupted by the shutdown and will fire any futureError listeners registered.

Parameters:
threadPool - the ThreadPoolExecutor to be shutdown.
Returns:
a list of Runnables that were waiting for execution and had not yet started.
FutureTalker's in this list will have been rejected and Callable's will have been cancelled.

getCallable

public java.util.concurrent.Callable<V> getCallable()
Returns the Callable that this task executes

Returns:
callable the callable task

done

protected void done()
fire the listeners when task terminates or is cancelled

Overrides:
done in class java.util.concurrent.FutureTask<V>

rejected

public void rejected(java.util.concurrent.RejectedExecutionException r)
Causes this future to report an ExecutionException with the given throwable as its cause, unless this Future has already been set or has been cancelled.

Parameters:
r - the RejectedExecutionException to be set as the error for this task.

addListener

public void addListener(IFutureListener<V> listener)
Adds a listener to this task.
If the task has already terminated then this listener is called immediately. Otherwise it is call on the termination of the task.

Each listener removed once it is called.

Any errors thrown by listeners are caught and ignored.

Parameters:
listener - listener to add. Duplicates quietly are ignored.
Throws:
java.lang.IllegalArgumentException - thrown if listener is null.

removeListener

public void removeListener(IFutureListener<V> listener)
Remove a listener from this task, if it is found.
Listeners not found in the listener list are ignored.

Parameters:
listener - the listener to remove.
Throws:
java.lang.IllegalArgumentException - thrown if listener is null.

fireListeners

protected void fireListeners()
Fires currently registered listeners and removes them.
This should only be called from FutureTask.done()

Try a Future.get() the result and then handle any exceptions that occure.
ExecutionException are un-wrapped and the underlying exception passed to futureError


©2004, Forward Computing and Control Pty. Ltd
ACN 003 669 994   NSW Australia
All Rights Reserved.