Class AsyncForEachRun<I,R>
- Type Parameters:
I- the type of the elements being iterated overR- the type of the final result after applying the thenApply function
This class is designed to work with other asynchronous interfaces and utility classes to enable complex asynchronous workflows. It allows for non-blocking execution of tasks, which can improve the performance and responsiveness of HDFS operations.
The class implements the AsyncRun interface, which means it can be used in asynchronous task chains. It maintains an Iterator of elements to process, an asyncDoOnce to apply to each element.
The run method initiates the asynchronous operation, and the doOnce method recursively applies the asyncDoOnce to each element and handles the results. If the shouldBreak flag is set, the operation is completed with the current result.
AsyncForEachRun is used to implement the following semantics:
for (I element : elements) {
R result = asyncDoOnce(element);
}
return result;
- See Also:
-
Field Summary
Fields inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async
CUR_COMPLETABLE_FUTURE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasyncDo(AsyncBiFunction<AsyncForEachRun<I, R>, I, R> asyncDo) Sets the async function to apply to each element from the iterator.voidbreakNow()Triggers the termination of the current asynchronous iteration.Sets the Iterator for the elements to be processed in the asynchronous operation.voidrun()Initiates the asynchronous foreach operation by starting the iteration process over the elements provided by the iterator.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.hadoop.hdfs.server.federation.router.async.utils.Async
getCurCompletableFuture, result, setCurCompletableFuture
-
Constructor Details
-
AsyncForEachRun
public AsyncForEachRun()
-
-
Method Details
-
run
public void run()Initiates the asynchronous foreach operation by starting the iteration process over the elements provided by the iterator. This method sets up the initial call to doOnce(R) with a null result, which begins the recursive application of the async function to each element of the iterator.This method is an implementation of the
AsyncRuninterface'srunmethod, allowing it to be used in a chain of asynchronous operations. It is responsible for starting the asynchronous processing and handling the completion of the operation through the internalCompletableFuture.If an exception occurs during the first call to
doOnce, the exception is caught and the internal CompletableFuture is completed exceptionally with aCompletionExceptionwrapping the original IOException.After initiating the operation, the method sets the current thread's
AsyncCompletableFutureby callingAsync.setCurCompletableFuture(CompletableFuture)with the internal result CompletableFuture. This allows other parts of the asynchronous workflow to chain further operations or handle the final result once the foreach loop completes. -
breakNow
public void breakNow()Triggers the termination of the current asynchronous iteration.This method is used to break out of the asynchronous for-each loop prematurely. It sets a flag that indicates the iteration should be terminated at the earliest opportunity. This is particularly useful when the processing logic determines that further iteration is unnecessary or when a specific condition has been met.
Once this method is called, the next time the loop is about to process a new element, it will check the flag and cease operation, allowing the application to move on to the next step or complete the task.
-
forEach
Sets the Iterator for the elements to be processed in the asynchronous operation.- Parameters:
forEach- The Iterator over the elements.- Returns:
- The current AsyncForEachRun instance for chaining.
-
asyncDo
Sets the async function to apply to each element from the iterator.- Parameters:
asyncDo- The async function.- Returns:
- The current AsyncForEachRun instance for chaining.
-