Class ActiveTask

java.lang.Object
com.pnfsoftware.jeb.util.concurrent.ActiveTask
All Implemented Interfaces:
Runnable

@Ser public abstract class ActiveTask extends Object implements Runnable
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 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.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Create an active task.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Request cancellation and return.
    boolean
    cancel(boolean interruptThread)
    Request cancellation and return.
    boolean
    Request an interruption of the task's thread.
    final boolean
    Determine whether cancellation was requested.
    boolean
    Determine whether this task is not running.
    boolean
    Wait indefinitely for the task thread to terminate.
    boolean
    join(long millis)
    Wait for the task thread to terminate.
    protected void
    Executed in async mode only (in sync mode, exceptions happening during execution are not caught).
    protected void
    Executed in sync or async mode.
    protected void
    Executed in sync or async mode.
    void
    run()
    Careful not to call when currently executed (sync or async).
    abstract void
    Execute the task body.
    boolean
    start(Runnable completion)
    Execute this task on a new daemon thread.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ActiveTask

      public ActiveTask(String name)
      Create an active task.
      Parameters:
      name - task name
  • Method Details

    • start

      public 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
    • interrupt

      public boolean interrupt()
      Request an interruption of the task's thread. This method is non blocking.
      Returns:
      true if the task thread was alive and was interrupted
    • cancel

      public boolean cancel()
      Request cancellation and return. The thread is interrupted.
      Returns:
      true if the thread was alive and has been cancelled
    • cancel

      public 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
    • isDone

      public boolean isDone()
      Determine whether this task is not running.
      Returns:
      true if no task thread exists or the task thread has terminated
    • isCancelled

      public final boolean isCancelled()
      Determine whether cancellation was requested.
      Returns:
      true if cancel() or cancel(boolean) was called
    • join

      public boolean join() throws InterruptedException
      Wait indefinitely for the task thread to terminate.
      Returns:
      true after the wait completed
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
    • join

      public boolean join(long millis) throws InterruptedException
      Wait for the task thread to terminate.
      Parameters:
      millis - maximum time to wait, in milliseconds; 0 means wait indefinitely
      Returns:
      true after the wait completed
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
    • run

      public void run()
      Careful not to call when currently executed (sync or async).
      Specified by:
      run in interface Runnable
    • runi

      public abstract void runi()
      Execute the task body.
    • onPreExecution

      protected void onPreExecution()
      Executed in sync or async mode. Default implementation does nothing.
    • onPostExecution

      protected void onPostExecution()
      Executed in sync or async mode. Default implementation does nothing.
    • onException

      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.
      Parameters:
      e - exception raised by runi()