I am running several hundred functions with runAsync(). All of the functions modify some statically available list and thus do not need to return anything. I want to make sure they all finish before continuing my processing. Is this the appropriate way to wait? Is there a simpler way to accomplish what I'm trying to do?
List<CompletableFuture<Void>> futures = new ArrayList<>();
active_sites.forEach(site -> site.getEntryPoints().stream().map(EntryPoint::scanEntryPoint).forEach(futures::add));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();
To get the result of this CompletableFuture we can call get () method. 3. Running a task in ‘fire and forget’ mode In some scenario we might need to fire the function and the result of the function call is not used in the current function. This can be achieved using CompletableFuture’s runAsync .
Creating a CompletableFuture Task First of all to create a new completableFuture task, simply call the constructor Or mark the task as completed by calling the complete method. All the clients waiting for task to complete will get the specified result. To get the result of this CompletableFuture we can call get () method. 3.
Use async/await to Wait for a Function to Finish Before Continuing Execution. Another way to wait for a function to execute before continuing the execution in the asynchronous environment in JavaScript is to use async/wait.
Another way to wait for a function to execute before continuing the execution in the asynchronous environment in JavaScript is to use async/wait.
You can simplify that quite a bit:
CompletableFuture[] scans = active_sites.stream()
.flatMap(site -> site.getEntryPoints().stream())
.map(EntryPoint::scanEntryPoint)
.toArray(CompletableFuture[]::new)
CompletableFuture.allOf(scans).join();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With