Interface AsyncApplyFunction<T,R>
- Type Parameters:
T- the type of the input to the functionR- the type of the result of the function
- All Superinterfaces:
ApplyFunction<T,,R> Async<R>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
ApplyFunction and is
designed to be used with asynchronous computation frameworks,
such as Java's CompletableFuture.
An implementation of this interface is expected to perform an
asynchronous operation and return a result, which is typically
represented as a CompletableFuture<R>. This allows for
non-blocking execution of tasks and is particularly useful for
I/O operations or any operation that may take a significant amount
of time to complete.
AsyncApplyFunction is used to implement the following semantics:
T res = doAsync1(input);
// Can use AsyncApplyFunction
R result = doAsync2(res);
- See Also:
-
Field Summary
Fields inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async
CUR_COMPLETABLE_FUTURE -
Method Summary
Modifier and TypeMethodDescriptiondefault CompletableFuture<R>apply(CompletableFuture<T> in) Asynchronously applies this function to the result of the given CompletableFuture.default CompletableFuture<R>apply(CompletableFuture<T> in, Executor executor) Asynchronously applies this function to the result of the given CompletableFuture, using the specified executor for the asynchronous computation.default RSynchronously applies this function to the given argument and returns the result.voidapplyAsync(T t) Asynchronously applies this function to the given argument.default CompletableFuture<R>Initiates the asynchronous application of this function to the given result.Methods inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async
getCurCompletableFuture, result, setCurCompletableFuture
-
Method Details
-
applyAsync
Asynchronously applies this function to the given argument.This method is intended to initiate the function application without waiting for the result. It is typically used when the result of the operation is not required immediately or when the operation is part of a larger asynchronous workflow.
- Parameters:
t- the function argument- Throws:
IOException- if an I/O error occurs during the application of the function
-
apply
Synchronously applies this function to the given argument and returns the result.This method waits for the asynchronous operation to complete and returns its result. It is useful when the result is needed immediately and the calling code cannot proceed without it.
- Specified by:
applyin interfaceApplyFunction<T,R> - Parameters:
t- the function argument- Returns:
- the result of applying the function to the argument
- Throws:
IOException- if an I/O error occurs during the application of the function
-
async
Initiates the asynchronous application of this function to the given result.This method calls applyAsync to start the asynchronous operation and then retrieves the current thread's CompletableFuture using getCurCompletableFuture. It returns this CompletableFuture, which will be completed with the result of the asynchronous operation once it is finished.
This method is useful for chaining with other asynchronous operations, as it allows the current operation to be part of a larger asynchronous workflow.
- Parameters:
t- the function argument- Returns:
- a CompletableFuture that will be completed with the result of the asynchronous operation
- Throws:
IOException- if an I/O error occurs during the initiation of the asynchronous operation
-
apply
Asynchronously applies this function to the result of the given CompletableFuture.This method chains the function application to the completion of the input future. It returns a new CompletableFuture that completes with the function's result when the input future completes.
- Specified by:
applyin interfaceApplyFunction<T,R> - Parameters:
in- the input future- Returns:
- a new CompletableFuture that holds the result of the function application
-
apply
Asynchronously applies this function to the result of the given CompletableFuture, using the specified executor for the asynchronous computation.This method allows for more control over the execution context of the asynchronous operation, such as running the operation in a separate thread or thread pool.
- Specified by:
applyin interfaceApplyFunction<T,R> - Parameters:
in- the input futureexecutor- the executor to use for the asynchronous computation- Returns:
- a new CompletableFuture that holds the result of the function application
-