Skip to content

Asynchronous execution/operation with CompletableFuture in Java 8+

In Java, is calling get() method while looping on CompletableFuture instances as good as doing synchronous operations although CompletableFuture is used for async calls?


‘get()’ waits until the future is completed. If that’s what you want, it’s what you use. There’s no general rule.

For example, you might be using a method that is inherently asynchronous, but in your particular use, you need to wait for it to complete. If so, then there’s nothing wrong with waiting for it to complete!

You mention a loop. You might find it applicable to start all the tasks in the loop, collecting a list of futures, and then (outside the loop) wait for them all to complete. That way you’re getting some parallelism.

But as a general rule: it depends.