CTH
CTH

Reputation: 77

Struggling with Java CompletionStage/CompletableFuture

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?

Upvotes: 1

Views: 1069

Answers (1)

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.

Upvotes: 3

Related Questions