Interface CancellationToken<E extends RuntimeException>

Type Parameters:
E - the exception type surfaced by throwIfCancellationRequested() and getCancellationFuture() when cancellation is requested

@Experimental public interface CancellationToken<E extends RuntimeException>
Token that allows asynchronous code to observe cancellation requests.
  • Method Details

    • isCancellationRequested

      boolean isCancellationRequested()
      Returns true after cancellation has been requested.
    • throwIfCancellationRequested

      void throwIfCancellationRequested() throws E
      Throws E if cancellation has been requested.
      Throws:
      E
    • getCancellationFuture

      default CompletableFuture<Void> getCancellationFuture()
      Future that completes exceptionally with E when cancellation has been requested.

      Code waiting on external work can chain off this future to abort in-flight requests when cancellation is requested.

    • onCancel

      Registers a callback to run when cancellation is requested, or immediately if already cancelled.
      Returns:
      a handle that removes the callback if cancellation has not happened yet.
    • none

      static <E extends RuntimeException> CancellationToken<E> none()
      A token that is never cancelled.