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(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 viaThread#interrupt()
. Always use #cancel() instead.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CancellableThreads.ExecutionCancelledException
static interface
CancellableThreads.Interruptible
static interface
CancellableThreads.IOInterruptible
static interface
CancellableThreads.OnCancel
-
Constructor Summary
Constructors Constructor Description CancellableThreads()
-
Method Summary
Modifier and Type Method Description void
cancel(java.lang.String reason)
cancel all current running operations.void
checkForCancel()
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
isCancelled()
void
setOnCancel(CancellableThreads.OnCancel onCancel)
Registers a callback that will be invoked when some running operations are cancelled orcheckForCancel()
is called.
-
-
-
Method Detail
-
isCancelled
public boolean isCancelled()
-
checkForCancel
public void checkForCancel()
-
execute
public void execute(CancellableThreads.Interruptible interruptible)
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
public void executeIO(CancellableThreads.IOInterruptible interruptible) throws java.io.IOException
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:
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
-
setOnCancel
public void setOnCancel(CancellableThreads.OnCancel onCancel)
Registers a callback that will be invoked when some running operations are cancelled orcheckForCancel()
is called.
-
-