public class FutureUtil extends Object
CompletableFuture.| Constructor and Description |
|---|
FutureUtil() |
| Modifier and Type | Method and Description |
|---|---|
static <T> CompletableFuture<T> |
addTimeoutHandling(CompletableFuture<T> future,
Duration timeout,
ScheduledExecutorService executor,
Supplier<Throwable> exceptionSupplier)
Adds timeout handling to an existing
CompletableFuture. |
static <T> CompletableFuture<T> |
createFutureWithTimeout(Duration timeout,
ScheduledExecutorService executor,
Supplier<Throwable> exceptionSupplier)
Creates a new
CompletableFuture instance with timeout handling. |
static TimeoutException |
createTimeoutException(String message,
Class<?> sourceClass,
String sourceMethod)
Creates a low-overhead timeout exception which is performance optimized to minimize allocations
and cpu consumption.
|
static <T> CompletableFuture<T> |
failedFuture(Throwable t) |
static Throwable |
unwrapCompletionException(Throwable t) |
static CompletableFuture<Void> |
waitForAll(List<? extends CompletableFuture<?>> futures)
Return a future that represents the completion of the futures in the provided list.
|
static CompletableFuture<Void> |
waitForAllAndSupportCancel(List<? extends CompletableFuture<?>> futures)
Return a future that represents the completion of the futures in the provided list.
|
static void |
whenCancelledOrTimedOut(CompletableFuture<?> future,
Runnable cancelAction)
If the future is cancelled or times out, the cancel action will be
invoked
The action is executed once if the future completes with
CancellationException or TimeoutException |
public static CompletableFuture<Void> waitForAll(List<? extends CompletableFuture<?>> futures)
futures - futures to wait forpublic static CompletableFuture<Void> waitForAllAndSupportCancel(List<? extends CompletableFuture<?>> futures)
CompletableFuture.cancel(boolean). It will cancel
all unfinished futures when the future gets cancelled.futures - futures to wait forpublic static void whenCancelledOrTimedOut(CompletableFuture<?> future, Runnable cancelAction)
CancellationException or TimeoutExceptionfuture - future to attach the action tocancelAction - action to invoke if the future is cancelled or times outpublic static <T> CompletableFuture<T> failedFuture(Throwable t)
public static <T> CompletableFuture<T> createFutureWithTimeout(Duration timeout, ScheduledExecutorService executor, Supplier<Throwable> exceptionSupplier)
CompletableFuture instance with timeout handling.T - type parameter for the futuretimeout - the duration of the timeoutexecutor - the executor to use for scheduling the timeoutexceptionSupplier - the supplier for creating the exceptionCompletableFuture instancepublic static <T> CompletableFuture<T> addTimeoutHandling(CompletableFuture<T> future, Duration timeout, ScheduledExecutorService executor, Supplier<Throwable> exceptionSupplier)
CompletableFuture.T - type parameter for the futurefuture - the target futuretimeout - the duration of the timeoutexecutor - the executor to use for scheduling the timeoutexceptionSupplier - the supplier for creating the exceptionpublic static TimeoutException createTimeoutException(String message, Class<?> sourceClass, String sourceMethod)
message - exception messagesourceClass - source class for manually filled in stacktracesourceMethod - source method name for manually filled in stacktraceCopyright © 2017–2021 Apache Software Foundation. All rights reserved.