Class InterruptableAction<T>

java.lang.Object
net.algart.contexts.InterruptableAction<T>

public abstract class InterruptableAction<T> extends Object

The class allowing to execute some action, interruptable via standard Java technique, in terms of some InterruptionContext. In other words, it is a bridge between InterruptionContext and standard interruption technique based on thread.interrupt() and Thread.interrupted() calls.

To use this class, please override its run() method and call doInterruptibly().

Author:
Daniel Alievsky
  • Constructor Details

    • InterruptableAction

      protected InterruptableAction(InterruptionContext context)
      Creates an instance of this class with the given interruption context.
      Parameters:
      context - an interruption context that will be used for interruption of the action.
      Throws:
      NullPointerException - if the argument is null.
  • Method Details

    • doInterruptibly

      public T doInterruptibly() throws InterruptionException
      Executes run() method and returns its result.

      If the interruption context requests an interruption (i.e. an interruption event occurs), the current thread, executing run() method, is interrupted by its interrupt() method. As a result, the run() method should stop and throw the standard InterruptedException. This method catches it and translates into InterruptionException with the corresponding cause; this InterruptionException is thrown.

      So, this method can be interrupted by both ways: by the standard thread.interrupt() call and by interruption mechanism provided by the InterruptionContext.

      Returns:
      the result of run() method.
      Throws:
      InterruptionException - if the run() method throws InterruptedException.
    • run

      public abstract T run() throws InterruptedException
      This method performs some action or throws InterruptedException in a case when the current thread is interrupted via thread.interrupt() call.
      Returns:
      the computed result.
      Throws:
      InterruptedException - if the current thread is interrupted via thread.interrupt() call.