Package org.elasticsearch.common.util
Class CancellableThreads
- java.lang.Object
-
- org.elasticsearch.common.util.CancellableThreads
-
public class CancellableThreads extends java.lang.Object
A utility class for multi threaded operation that needs to be cancellable via interrupts. Every cancellable operation should be executed viaexecute(Interruptable)
, 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 viaThread#interrupt()
. Always use #cancel() instead.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CancellableThreads.ExecutionCancelledException
static interface
CancellableThreads.Interruptable
static interface
CancellableThreads.IOInterruptable
-
Constructor Summary
Constructors Constructor Description CancellableThreads()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cancel(java.lang.String reason)
cancel all current running operations.void
checkForCancel()
call this will throw an exception if operation was cancelled.void
execute(CancellableThreads.Interruptable interruptable)
run the Interruptable, capturing the executing thread.void
executeIO(CancellableThreads.IOInterruptable interruptable)
run the Interruptable, capturing the executing thread.boolean
isCancelled()
protected void
onCancel(java.lang.String reason, java.lang.Exception suppressedException)
called ifcheckForCancel()
was invoked after the operation was cancelled.
-
-
-
Method Detail
-
isCancelled
public boolean isCancelled()
-
checkForCancel
public void checkForCancel()
call this will throw an exception if operation was cancelled. OverrideonCancel(String, Exception)
for custom failure logic
-
onCancel
protected void onCancel(java.lang.String reason, @Nullable java.lang.Exception suppressedException)
called ifcheckForCancel()
was invoked after the operation was cancelled. the default implementation always throws anCancellableThreads.ExecutionCancelledException
, suppressing any other exception that occurred before cancellation- Parameters:
reason
- reason for failure supplied by the caller ofcancel(java.lang.String)
suppressedException
- any error that was encountered during the execution before the operation was cancelled.
-
execute
public void execute(CancellableThreads.Interruptable interruptable)
run the Interruptable, capturing the executing thread. Concurrent calls tocancel(String)
will interrupt this thread causing the call to prematurely return.- Parameters:
interruptable
- code to run
-
executeIO
public void executeIO(CancellableThreads.IOInterruptable interruptable) throws java.io.IOException
run the Interruptable, capturing the executing thread. Concurrent calls tocancel(String)
will interrupt this thread causing the call to prematurely return.- Parameters:
interruptable
- code to run- Throws:
java.io.IOException
-
cancel
public void cancel(java.lang.String reason)
cancel all current running operations. Future calls tocheckForCancel()
will be failed with the given reason
-
-