java.util.concurrent
Class CancellableTask.InnerCancellableFuture<V>

java.lang.Object
  extended byjava.util.concurrent.CancellableTask.InnerCancellableFuture
All Implemented Interfaces:
Cancellable, Future, java.lang.Runnable
Enclosing class:
CancellableTask

protected class CancellableTask.InnerCancellableFuture<V>
extends java.lang.Object
implements Future<V>, java.lang.Runnable

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

CancellableTask.InnerCancellableFuture

protected CancellableTask.InnerCancellableFuture(Callable<V> callable)
Create an InnerCancellableFuture that will execute the given callable.

Parameters:
callable - the function to execute
Method Detail

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Description copied from interface: Cancellable
Attempt to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the interruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

Specified by:
cancel in interface Cancellable
Parameters:
mayInterruptIfRunning - true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete
Returns:
false if the task could not be cancelled, typically because is has already completed normally; true otherwise

isCancelled

public boolean isCancelled()
Description copied from interface: Cancellable
Returns true if this task was cancelled before it completed normally.

Specified by:
isCancelled in interface Cancellable
Returns:
true if task was cancelled before it completed

isDone

public boolean isDone()
Description copied from interface: Cancellable
Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.

Specified by:
isDone in interface Cancellable
Returns:
true if this task completed.

run

public void run()
Sets this Future to the results of callable.call

Specified by:
run in interface java.lang.Runnable

get

public V get()
      throws java.lang.InterruptedException,
             ExecutionException
Waits if necessary for the call to callable.call to complete, and then retrieves its result.

Specified by:
get in interface Future
Returns:
computed result
Throws:
CancellationException - here???
ExecutionException - if underlying computation threw an exception
java.lang.InterruptedException - if current thread was interrupted while waiting

get

public V get(long timeout,
             TimeUnit unit)
      throws java.lang.InterruptedException,
             ExecutionException,
             TimeoutException
Waits if necessary for at most the given time for the call to callable.call to complete, and then retrieves its result.

Specified by:
get in interface Future
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
computed result
Throws:
ExecutionException - if underlying computation threw an exception
java.lang.InterruptedException - if current thread was interrupted while waiting
TimeoutException - if the wait timed out

set

protected void set(V v)
Sets the result of this Future to the given value.

Parameters:
v - the value

setException

protected void setException(java.lang.Throwable t)
Causes this future to report an ExecutionException with the given throwable as its cause.

Parameters:
t - the cause of failure.