Class InterruptableAction<T>
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 Summary
ModifierConstructorDescriptionprotected
InterruptableAction
(InterruptionContext context) Creates an instance of this class with the given interruption context. -
Method Summary
-
Constructor Details
-
InterruptableAction
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
Executesrun()
method and returns its result.If the interruption context requests an interruption (i.e. an
interruption event
occurs), the current thread, executingrun()
method, is interrupted by its interrupt() method. As a result, therun()
method should stop and throw the standard InterruptedException. This method catches it and translates intoInterruptionException
with the corresponding cause; thisInterruptionException
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 therun()
method throws InterruptedException.
-
run
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.
-