Class 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 via execute(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 via Thread#interrupt(). Always use #cancel() instead.
    • Constructor Detail

      • CancellableThreads

        public CancellableThreads()
    • Method Detail

      • isCancelled

        public boolean isCancelled()
      • checkForCancel

        public void checkForCancel()
        call this will throw an exception if operation was cancelled. Override onCancel(String, Exception) for custom failure logic
      • onCancel

        protected void onCancel​(java.lang.String reason,
                                @Nullable
                                java.lang.Exception suppressedException)
        called if checkForCancel() was invoked after the operation was cancelled. the default implementation always throws an CancellableThreads.ExecutionCancelledException, suppressing any other exception that occurred before cancellation
        Parameters:
        reason - reason for failure supplied by the caller of cancel(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 to cancel(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 to cancel(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 to checkForCancel() will be failed with the given reason