|
FutureTalker V1.3 by Matthew Ford 2005/04/11 |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.concurrent.FutureTask<V>
au.com.forward.futureTalker.FutureTalker<V>
public class FutureTalker<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 FutureTalkerwhere callable implements Callable(callable)); talker.addListener(listener); try { threadPool.execute(talker); } catch (RejectedExecutionException rex) { talker.rejected(rex); // failed to execute set task to error and notifies listeners }
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 |
---|
protected java.util.List<IFutureListener<V>> listenerList
Constructor Detail |
---|
public FutureTalker(java.util.concurrent.Callable<V> callable)
callable
- the callable task
java.lang.NullPointerException
- if callable is nullMethod Detail |
---|
public static void ifInterruptedStop() throws java.lang.InterruptedException
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.
java.lang.InterruptedException
- Thrown if the interrupt flag is set for the thread running this method.public static java.util.List<java.lang.Runnable> shutdownNow(java.util.concurrent.ThreadPoolExecutor threadPool)
ThreadPoolExecutor
and reject or cancel unstarted pending tasks.
futureError
listeners registered.
threadPool
- the ThreadPoolExecutor
to
be shutdown.
FutureTalker
's in this list will have been rejected and Callable
's will have been cancelled.public java.util.concurrent.Callable<V> getCallable()
protected void done()
done
in class java.util.concurrent.FutureTask<V>
public void rejected(java.util.concurrent.RejectedExecutionException r)
r
- the RejectedExecutionException
to be set as the error for this task.public void addListener(IFutureListener<V> listener)
Each listener removed once it is called.
Any errors thrown by listeners are caught and ignored.
listener
- listener to add. Duplicates quietly are ignored.
java.lang.IllegalArgumentException
- thrown if listener is null.public void removeListener(IFutureListener<V> listener)
listener
- the listener to remove.
java.lang.IllegalArgumentException
- thrown if listener is null.protected void fireListeners()
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. |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |