0

Looking at a the docs and a couple of examples, im still a little confused. I think I get the bare basics of it, but Im confused on when one would use them.

I guess my main questions..

  1. Are CompletionStage/CompletableFuture both for async code? If so, why would you use one over the other?
  2. If CompletionStage can be used for non-async code, why even use it at all? How would it be any different than just standard sequential code?
  3. How are they even different? I see there are asynchronous methods you can call for both CompletionStage and CompletableFuture.
  4. What scenario would you use one over the other?
CTH
  • 75
  • 4

1 Answers1

2

CompletionStage is an interface that (1) can be implemented by any number of classes and (2) does not specify detailed policies such as the thread allocation of asynchronous operations.

CompletableFuture is a class that implements CompletionStage (and Future) and does provide more detail about those policies.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • Yeah, I understand that for the most part. But that doesnt answer my questions.. mainly: If CompletionStage can be used for non-async code, why even use it at all? How would it be any different than just standard sequential code? – CTH Nov 27 '20 at 21:23
  • 3
    @Taylor It allows you to _not care_. – chrylis -cautiouslyoptimistic- Nov 27 '20 at 21:28
  • just in case you need synchronous access to the variable which can be not ready yet. What other method do you think of? – Alexei Kaigorodov Nov 28 '20 at 10:39
  • code itself is not sync or async per se. It can be sequential or parallel. It is the access to the result of parallel code which can be synchronous or asynchronous. – Alexei Kaigorodov Nov 28 '20 at 10:42