In Java, is calling get() method while looping on CompletableFuture instances as good as doing synchronous operations although CompletableFuture is used for async calls?
Asked
Active
Viewed 186 times
-1

Mark Rotteveel
- 100,966
- 191
- 140
- 197

AdityaKapreShrewsburyBoston
- 1,143
- 2
- 16
- 37
-
1It is as *bad* as doing synchronous operations. – Louis Wasserman Aug 08 '21 at 14:41
1 Answers
1
'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.

iggy
- 1,328
- 4
- 3
-
Thanks for your answer @iggy. I meant collecting futures and then calling get method in loop on each future while iterating futures. – AdityaKapreShrewsburyBoston Aug 08 '21 at 16:30
-
1If you loop body contains `someValue = doSomething().get()` then you're running them sequentially and synchronously. – iggy Aug 08 '21 at 18:13