java.util.concurrent
Class CancellableTask

java.lang.Object
  extended by java.util.concurrent.CancellableTask
All Implemented Interfaces:
Cancellable, Runnable
Direct Known Subclasses:
FutureTask

public class CancellableTask
extends Object
implements Cancellable, Runnable

Base class for Cancellable Runnable actions within the Executor framework. In addition to serving as a standalone class, this provides protected functionality that may be useful when creating customized task classes.

Since:
1.5

Nested Class Summary
protected  class CancellableTask.InnerCancellableFuture<V>
          Implementation of Future methods under the control of a current CancellableTask, which it relies on for methods isDone, isCancelled and cancel.
 
Constructor Summary
protected CancellableTask()
          Creates a new CancellableTask without a runnable action, which must be set using setRunnable before use.
  CancellableTask(Runnable r)
          Creates a new CancellableTask which invokes the given Runnable when executed.
 
Method Summary
 boolean cancel(boolean mayInterruptIfRunning)
          Attempt to cancel execution of this task.
protected  void done()
          Protected method invoked when this task transitions to state isDone (whether normally or via cancellation).
protected  Runnable getRunnable()
          Return the Runnable forming the basis of this task.
 boolean isCancelled()
          Returns true if this task was cancelled before it completed normally.
 boolean isDone()
          Returns true if this task completed.
protected  boolean reset()
          Reset the run state of this task to its initial state unless it has been cancelled.
 void run()
          Runs the runnable if not cancelled, maintaining run status.
protected  void setCancelled()
          Set the state of this task to Cancelled.
protected  void setDone()
          Set the state of this task to Done, unless already in a Cancelled state, in which Cancelled status is preserved.
protected  void setRunnable(Runnable r)
          Set the Runnable forming the basis of this task.
protected  boolean setRunning()
          Attempt to set the state of this task to Running, succeeding only if the state is currently NOT Done, Running, or Cancelled.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CancellableTask

public CancellableTask(Runnable r)
Creates a new CancellableTask which invokes the given Runnable when executed.

Parameters:
r - the runnable action
Throws:
NullPointerException - if runnable is null

CancellableTask

protected CancellableTask()
Creates a new CancellableTask without a runnable action, which must be set using setRunnable before use. This is intended for use in subclasses that must complete superclass construction before establishing the runnable action.

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.

getRunnable

protected Runnable getRunnable()
Return the Runnable forming the basis of this task.

Returns:
the runnable action
See Also:
setRunnable(java.lang.Runnable)

setRunnable

protected void setRunnable(Runnable r)
Set the Runnable forming the basis of this task.

Parameters:
r - the runnable action
See Also:
getRunnable()

setCancelled

protected void setCancelled()
Set the state of this task to Cancelled.


setDone

protected void setDone()
Set the state of this task to Done, unless already in a Cancelled state, in which Cancelled status is preserved.


setRunning

protected boolean setRunning()
Attempt to set the state of this task to Running, succeeding only if the state is currently NOT Done, Running, or Cancelled.

Returns:
true if successful

run

public void run()
Runs the runnable if not cancelled, maintaining run status.

Specified by:
run in interface Runnable
See Also:
Thread.run()

done

protected void done()
Protected method invoked when this task transitions to state isDone (whether normally or via cancellation). The default implementation does nothing. Subclasses may override this method to invoke completion callbacks or perform bookkeeping. Note that you can query status inside the implementation of this method to determine whether this task has been cancelled.


reset

protected boolean reset()
Reset the run state of this task to its initial state unless it has been cancelled. (Note that a cancelled task cannot be reset.)

Returns:
true if successful