public class CompletableFutureCancellationHandler extends Object
This class ensures that the cancel action gets called once after the future completes with
either CancellationException or TimeoutException.
The implementation handles possible race conditions that
might happen when the future gets cancelled before the cancel action is set to this handler.
For timeouts, CompletableFuture's "orTimeout" method introduced in JDK9 can be used in client code.
Cancellation and timeout support will only be active on the future where the cancellation handler has been attached to. Cancellation won't happen if .cancel is called on any "downstream" dependent futures. A cancellation or timeout that happens in any "upstream" future will get handled.
| Constructor and Description |
|---|
CompletableFutureCancellationHandler() |
| Modifier and Type | Method and Description |
|---|---|
void |
attachToFuture(CompletableFuture<?> future)
Attaches the cancellation handler to handle cancels
and timeouts.
|
<T> CompletableFuture<T> |
createFuture()
Creates a new
CompletableFuture and attaches the cancellation handler
to handle cancels and timeouts. |
void |
setCancelAction(Runnable cancelAction)
Set the action to run when the future gets cancelled or timeouts.
|
public CompletableFutureCancellationHandler()
public <T> CompletableFuture<T> createFuture()
CompletableFuture and attaches the cancellation handler
to handle cancels and timeouts.T - the result type of the futurepublic void attachToFuture(CompletableFuture<?> future)
future - the future to attach the handler topublic void setCancelAction(Runnable cancelAction)
cancelAction - the action to run when the the future gets cancelled or timeoutsCopyright © 2017–2021 Apache Software Foundation. All rights reserved.