public abstract class

ActiveTask

extends Object
implements Runnable
java.lang.Object
   ↳ com.pnfsoftware.jeb.util.concurrent.ActiveTask

Class Overview

A runnable that can control its own execution. Tasks extending this class must implement the runi() method. Client code can start a task, request cancellation and/or interruption, as well as join for the task to terminate (either normal termination or a forced termination resulting from a cancel/interrupt request).

This class is similar to some implementation of Future futures (albeit, its interface is simpler). The underlying execution primitive (the thread) is indirectly accessible via the join() method; in particular, this class should be used if the client code wants to explicitly join on the underlying thread.

Summary

Public Constructors
ActiveTask(String name)
ActiveTask()
Public Methods
synchronized boolean cancel()
Request cancellation and return.
synchronized boolean cancel(boolean interruptThread)
Request cancellation and return.
synchronized boolean interrupt()
Request an interruption of the task's thread.
final boolean isCancelled()
synchronized boolean isDone()
synchronized boolean join()
synchronized boolean join(long millis)
void run()
Careful not to call when currently executed (sync or async).
abstract void runi()
synchronized boolean start(Runnable completion)
Execute this task on a new daemon thread.
Protected Methods
void onException(Exception e)
Executed in async mode only (in sync mode, exceptions happening during execution are not caught).
void onPostExecution()
Executed in sync or async mode.
void onPreExecution()
Executed in sync or async mode.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Runnable

Public Constructors

public ActiveTask (String name)

Parameters
name task name

public ActiveTask ()

Public Methods

public synchronized boolean cancel ()

Request cancellation and return. The thread is interrupted.

Returns
  • true if the thread was alive and has been cancelled

public synchronized boolean cancel (boolean interruptThread)

Request cancellation and return.

Parameters
interruptThread true to also interrupt the thread
Returns
  • true if the thread was alive and has been cancelled

public synchronized boolean interrupt ()

Request an interruption of the task's thread. This method is non blocking.

public final boolean isCancelled ()

public synchronized boolean isDone ()

public synchronized boolean join ()

Throws
InterruptedException

public synchronized boolean join (long millis)

Throws
InterruptedException

public void run ()

Careful not to call when currently executed (sync or async).

public abstract void runi ()

public synchronized boolean start (Runnable completion)

Execute this task on a new daemon thread. If a task has already been started and has not completed, a new task will not be started and instead this method will return false.

Parameters
completion optional runnable that will be executed upon completion, no matter the outcome (even if an exception raised); the execution will take place on the task's thread
Returns
  • true if a new task was started, false otherwise

Protected Methods

protected void onException (Exception e)

Executed in async mode only (in sync mode, exceptions happening during execution are not caught). The default implementation logs the error and does not rethrow it.

protected void onPostExecution ()

Executed in sync or async mode. Default implementation does nothing.

protected void onPreExecution ()

Executed in sync or async mode. Default implementation does nothing.