Package org.opensearch.common.util
Class CancellableThreads
java.lang.Object
org.opensearch.common.util.CancellableThreads
A utility class for multi threaded operation that needs to be cancellable via interrupts. Every cancellable operation should be
executed via
execute(Interruptible)
, which will capture the executing thread and make sure it is interrupted in the case
of cancellation.
Cancellation policy: This class does not support external interruption via Thread#interrupt()
. Always use #cancel() instead.
- Opensearch.api:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Thrown if there is an error cancelling executionstatic interface
Interruptible interfacestatic interface
IO Interruptiblestatic interface
Called when a thread is cancelled -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
cancel all current running operations.void
void
execute
(CancellableThreads.Interruptible interruptible) run the Interruptible, capturing the executing thread.void
executeIO
(CancellableThreads.IOInterruptible interruptible) run the Interruptible, capturing the executing thread.boolean
void
setOnCancel
(CancellableThreads.OnCancel onCancel) Registers a callback that will be invoked when some running operations are cancelled orcheckForCancel()
is called.
-
Constructor Details
-
CancellableThreads
public CancellableThreads()
-
-
Method Details
-
isCancelled
public boolean isCancelled() -
checkForCancel
public void checkForCancel() -
execute
run the Interruptible, capturing the executing thread. Concurrent calls tocancel(String)
will interrupt this thread causing the call to prematurely return.- Parameters:
interruptible
- code to run
-
executeIO
run the Interruptible, capturing the executing thread. Concurrent calls tocancel(String)
will interrupt this thread causing the call to prematurely return.- Parameters:
interruptible
- code to run- Throws:
IOException
-
cancel
cancel all current running operations. Future calls tocheckForCancel()
will be failed with the given reason -
setOnCancel
Registers a callback that will be invoked when some running operations are cancelled orcheckForCancel()
is called.
-