Execution Cancellation

  1. Cooperative Cancellation
  2. Cooperative Interruption
  3. Limitations
  4. Interruption Support

Failsafe supports cancellation and optional interruption of executions. Cancellation and interruption can be triggered by a Timeout or through an async execution’s Future:

Future<Connection> future = Failsafe.with(retryPolicy).getAsync(this::connect);
future.cancel(shouldInterrupt);

Cancellation will cause any async execution retries and timeout attempts to stop. Interruption will cause the execution thread’s interrupt flag to be set.

Cooperative Cancellation

Executions can cooperate with a cancellation by checking ExecutionContext.isCancelled():

Failsafe.with(timeout).getAsync(ctx -> {
  while (!ctx.isCancelled())
    doWork();
});

Cooperative Interruption

Execution interruption will cause certain blocking calls to unblock and may throw InterruptedException within your execution.

Non-blocking executions can cooperate with interruption by periodically checking Thread.isInterrupted():

Failsafe.with(timeout).getAsync(()-> {
  while (!Thread.isInterrupted())
    doBlockingWork();
});

Limitations

Since the async integration methods involve external threads, which Failsafe has no knowledge of, these executions cannot be directly cancelled or interrupted by Failsafe. These executions can still cooperate with cancellation as described above, though they cannot cooperate with interruption.

Interruption Support

Failsafe adds interruption support for any ForkJoinPool that is configured as an executor, including the common ForkJoinPool which is used by default. This means asynchronous tasks which are not normally interruptable outside of Failsafe can become interruptable when using Failsafe.