类 FutureUtil
java.lang.Object
org.apache.pulsar.common.util.FutureUtil
This class is aimed at simplifying work with
CompletableFuture.-
嵌套类概要
嵌套类 -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static <T> CompletableFuture<T>addTimeoutHandling(CompletableFuture<T> future, Duration timeout, ScheduledExecutorService executor, Supplier<Throwable> exceptionSupplier) Adds timeout handling to an existingCompletableFuture.static <T> voidcompleteAfter(CompletableFuture<T> dest, CompletableFuture<T> src) Make the dest future complete after another one.static voidcompleteAfterAll(CompletableFuture<Void> dest, CompletableFuture<? extends Object>... src) Make the dest future complete after others.static <T> CompletableFuture<T>composeAsync(Supplier<CompletableFuture<T>> futureSupplier, Executor executor) static <T> CompletableFuture<T>createFutureWithTimeout(Duration timeout, ScheduledExecutorService executor, Supplier<Throwable> exceptionSupplier) Creates a newCompletableFutureinstance with timeout handling.static TimeoutExceptioncreateTimeoutException(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>getException(CompletableFuture<T> future) static CompletableFuture<Void>runWithCurrentThread(Runnable runnable) static voidsafeRunAsync(Runnable runnable, Executor executor, CompletableFuture completableFuture) Executes an operation using the suppliedExecutorand notify failures on the suppliedCompletableFuture.static Throwablestatic CompletableFuture<Void>waitForAll(Collection<? extends CompletableFuture<?>> futures) Return a future that represents the completion of the futures in the provided Collection.static <T> CompletableFuture<List<T>>waitForAll(Stream<CompletableFuture<List<T>>> futures) static CompletableFuture<Void>waitForAllAndSupportCancel(Collection<? extends CompletableFuture<?>> futures) Return a future that represents the completion of the futures in the provided Collection.static CompletableFuture<Object>waitForAny(Collection<? extends CompletableFuture<?>> futures) Return a future that represents the completion of any future in the provided Collection.static CompletableFuture<Optional<Object>>waitForAny(Collection<? extends CompletableFuture<?>> futures, Predicate<Object> tester) Return a future that represents the completion of any future that match the predicate in the provided Collection.static voidwhenCancelledOrTimedOut(CompletableFuture<?> future, Runnable cancelAction) If the future is cancelled or times out, the cancel action will be invoked.static CompletionExceptionwrapToCompletionException(Throwable throwable) Wrap throwable exception to CompletionException if that exception is not an instance of CompletionException.
-
构造器详细资料
-
FutureUtil
public FutureUtil()
-
-
方法详细资料
-
waitForAll
public static CompletableFuture<Void> waitForAll(Collection<? extends CompletableFuture<?>> futures) Return a future that represents the completion of the futures in the provided Collection.- 参数:
futures- futures to wait for- 返回:
- a new CompletableFuture that is completed when all of the given CompletableFutures complete
-
runWithCurrentThread
-
waitForAll
-
completeAfter
Make the dest future complete after another one. is will be completed with the same value as , or be completed with the same error as . -
completeAfterAll
public static void completeAfterAll(CompletableFuture<Void> dest, CompletableFuture<? extends Object>... src) Make the dest future complete after others. is will be completed with aVoidvalue if all the futures of is completed, or be completed exceptionally with the same error as the first one completed exceptionally future of . -
waitForAny
public static CompletableFuture<Object> waitForAny(Collection<? extends CompletableFuture<?>> futures) Return a future that represents the completion of any future in the provided Collection.- 参数:
futures- futures to wait any- 返回:
- a new CompletableFuture that is completed when any of the given CompletableFutures complete
-
waitForAny
public static CompletableFuture<Optional<Object>> waitForAny(Collection<? extends CompletableFuture<?>> futures, Predicate<Object> tester) Return a future that represents the completion of any future that match the predicate in the provided Collection.- 参数:
futures- futures to wait anytester- if any future match the predicate- 返回:
- a new CompletableFuture that is completed when any of the given CompletableFutures match the tester
-
waitForAllAndSupportCancel
public static CompletableFuture<Void> waitForAllAndSupportCancel(Collection<? extends CompletableFuture<?>> futures) Return a future that represents the completion of the futures in the provided Collection. The future will supportCompletableFuture.cancel(boolean). It will cancel all unfinished futures when the future gets cancelled.- 参数:
futures- futures to wait for- 返回:
- a new CompletableFuture that is completed when all of the given CompletableFutures complete
-
whenCancelledOrTimedOut
If the future is cancelled or times out, the cancel action will be invoked. The action is executed once if the future completes withCancellationExceptionorTimeoutException- 参数:
future- future to attach the action tocancelAction- action to invoke if the future is cancelled or times out
-
failedFuture
-
unwrapCompletionException
-
createFutureWithTimeout
public static <T> CompletableFuture<T> createFutureWithTimeout(Duration timeout, ScheduledExecutorService executor, Supplier<Throwable> exceptionSupplier) Creates a newCompletableFutureinstance with timeout handling.- 类型参数:
T- type parameter for the future- 参数:
timeout- the duration of the timeoutexecutor- the executor to use for scheduling the timeoutexceptionSupplier- the supplier for creating the exception- 返回:
- the new
CompletableFutureinstance
-
addTimeoutHandling
public static <T> CompletableFuture<T> addTimeoutHandling(CompletableFuture<T> future, Duration timeout, ScheduledExecutorService executor, Supplier<Throwable> exceptionSupplier) Adds timeout handling to an existingCompletableFuture.- 类型参数:
T- type parameter for the future- 参数:
future- the target futuretimeout- the duration of the timeoutexecutor- the executor to use for scheduling the timeoutexceptionSupplier- the supplier for creating the exception- 返回:
- returns the original target future
-
composeAsync
@Nonnull public static <T> CompletableFuture<T> composeAsync(Supplier<CompletableFuture<T>> futureSupplier, Executor executor) - 抛出:
RejectedExecutionException- if this task cannot be accepted for executionNullPointerException- if one of params is null
-
createTimeoutException
public 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. It sets the stacktrace of the exception to the given source class and source method name. The instances of this class can be cached or stored as constants and reused multiple times.- 参数:
message- exception messagesourceClass- source class for manually filled in stacktracesourceMethod- source method name for manually filled in stacktrace- 返回:
- new TimeoutException instance
-
getException
-
wrapToCompletionException
Wrap throwable exception to CompletionException if that exception is not an instance of CompletionException.- 参数:
throwable- Exception- 返回:
- CompletionException
-
safeRunAsync
public static void safeRunAsync(Runnable runnable, Executor executor, CompletableFuture completableFuture) Executes an operation using the suppliedExecutorand notify failures on the suppliedCompletableFuture.- 参数:
runnable- the runnable to executeexecutor- the executor to use for executing the runnablecompletableFuture- the future to complete in case of exceptions
-