|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.concurrent.CancellableTask.InnerCancellableFuture
Implementation of Future methods under the control of a current CancellableTask, which it relies on for methods isDone, isCancelled and cancel. This class is split into an inner class to permit Future support to be mixed-in with other flavors of tasks. Normally, such a class will delegate Future get methods to the InnerCancellableFuture, and internally arrange that set methods be invoked when computations are ready.
Sample Usage. Here are fragments of an example subclass.
class MyFutureTask<V> extends CancellableTask implements Future<
V> {
MyFutureTask(Callable<V> callable) {
setRunnable(new InnerCancellableFuture<V>(callable));
}
public V get() throws InterruptedException, ExecutionException {
return ((InnerCancellableFuture<V>)getRunnable()).get();
}
// (And similarly for timeout version.)
void action() { // whatever action causes execution
try {
((InnerCancellableFuture<V>)getRunnable()).set(compute());
} catch (Exception ex) {
((InnerCancellableFuture<V>)getRunnable()).setException(ex);
}
}
}
| Constructor Summary | |
protected |
CancellableTask.InnerCancellableFuture(Callable<V> callable)
Create an InnerCancellableFuture that will execute the given callable. |
| Method Summary | |
boolean |
cancel(boolean mayInterruptIfRunning)
Attempt to cancel execution of this task. |
V |
get()
Waits if necessary for the call to callable.call to complete, and then retrieves its result. |
V |
get(long timeout,
TimeUnit unit)
Waits if necessary for at most the given time for the call to callable.call to complete, and then retrieves its result. |
boolean |
isCancelled()
Returns true if this task was cancelled before it completed normally. |
boolean |
isDone()
Returns true if this task completed. |
void |
run()
Sets this Future to the results of callable.call |
protected void |
set(V v)
Sets the result of this Future to the given value. |
protected void |
setException(java.lang.Throwable t)
Causes this future to report an ExecutionException with the given throwable as its cause. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
protected CancellableTask.InnerCancellableFuture(Callable<V> callable)
callable - the function to execute| Method Detail |
public boolean cancel(boolean mayInterruptIfRunning)
Cancellable
cancel in interface CancellablemayInterruptIfRunning - true if the thread executing this
task should be interrupted; otherwise, in-progress tasks are allowed
to complete
public boolean isCancelled()
Cancellable
isCancelled in interface Cancellablepublic boolean isDone()
Cancellable
isDone in interface Cancellablepublic void run()
run in interface java.lang.Runnable
public V get()
throws java.lang.InterruptedException,
ExecutionException
get in interface FutureCancellationException - here???
ExecutionException - if underlying computation threw an
exception
java.lang.InterruptedException - if current thread was interrupted
while waiting
public V get(long timeout,
TimeUnit unit)
throws java.lang.InterruptedException,
ExecutionException,
TimeoutException
get in interface Futuretimeout - the maximum time to waitunit - the time unit of the timeout argument
ExecutionException - if underlying computation threw an
exception
java.lang.InterruptedException - if current thread was interrupted
while waiting
TimeoutException - if the wait timed outprotected void set(V v)
v - the valueprotected void setException(java.lang.Throwable t)
t - the cause of failure.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||