Execution Cancellation

  1. Handling Cancellation
  2. Handling Interruption

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.

Handling Cancellation

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

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

Handling 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(ctx -> {
  while (!Thread.isInterrupted())
    doBlockingWork();
});