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(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.
    • Constructor Detail

      • CancellableThreads

        public CancellableThreads()
    • 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 to cancel(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 to cancel(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 to checkForCancel() will be failed with the given reason