Waiting on a list of Future
Use the CompletableFuture.allOf()
method to make sure all of your Futures have completed. You can then proceed to gather the results with a combination of the stream and map methods, which will take place once all futures have been finalized.
This quickly enables waiting for multiple concurrent tasks and retrieving the results.
Managing futures efficiently
Up the ante using ExecutorCompletionService
If you wish to manage Futures more efficiently, consider using ExecutorCompletionService
alongside an Executor
. This enables fetching the futures as soon as they're completed without having to wait for slower tasks.
You can submit tasks using a loop and pull completed tasks using completionService.take()
. Here's how:
Face the exceptions head on
Exceptional situations are bound to occur when dealing with multiple concurrent tasks. With the help of a try-catch block inside the loop fetching from the CompletionService
, exceptions can be managed effectively. In case of errors, cancel the remaining futures to save resources and maintain optimal performance.
Play safe with fast fail strategies
If your system needs to be quickly responsive to failed futures, consider CompletableFuture.anyOf()
. This method awaits the completion of at least one Future, bailing out from waiting on the remainder.
Building robust and responsive applications
Save your resources by cancelling on exception
In an exception situation, consider whether it's necessary to cancel remaining futures. This can save significant processing resources especially important for systems where every CPU cycle is a precious commodity.
Enjoy async ride with CompletableFuture
In async operations, make use of CompletableFuture
and its integrated exception handling mechanisms for building resilient systems.
Be wary of pitfalls with allOf
Bear in mind that CompletableFuture.allOf()
may not be your friend when dealing with exceptions. It waits for all futures, which might not be suitable if immediate exception handling is what you are looking for.
Was this article helpful?