If you really want to wait on all futures, you can simply call join()
on each of them:
growSeedFutureList.forEach(CompletableFuture::join);
The main difference compared to using allOf()
is that this will throw an exception as soon as it reaches a future completed with an exception, whereas the allOf().join()
version will only throw an exception after all futures have completed (exceptionally or not).
Another small difference is that this does not create the intermediary allOf
stage. Such a stage remains useful if you want to do something asynchronously after all futures have completed, instead of just waiting for all of them to complete.
The solution with the executor on the other side has several drawbacks:
- it prevents to reuse the executor as it requires its shutdown;
- it requires you to use that executor for all operations – it will not work with
CompletableFuture
s that are managed in another way; - it does not clearly shows your intent, which is to wait for all futures to complete;
- it is more complex to implement;
- it does not handle exceptional completion – no exception will be thrown by
awaitTermination()
if one of the tasks failed.