Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between 'CompletionStage' and 'CompletableFuture'

I have seen an example in each of them, but I need to know exactly what is the difference in deep, Because sometimes I think I can use both of them to get the same result, So I want know so that I can choose the correct one?

What is the benefit of using each of them?

Like this example both works:

public CompletionStage<Result> getNextQueryUUID() {
    return CompletableFuture.supplyAsync(() -> {
        String nextId = dbRequestService.getNextRequestQueryUUID();
        return ok(nextId);
    }, executor);
}


public CompletableFuture<Result> getNextQueryUUID() {
    return CompletableFuture.supplyAsync(() -> {
        String nextId = dbRequestService.getNextRequestQueryUUID();
        return ok(nextId);
    }, executor);
}

This example run in Play framework.

like image 731
Ebraheem Alrabeea Avatar asked Nov 30 '17 10:11

Ebraheem Alrabeea


People also ask

What is a CompletionStage?

public interface CompletionStage<T> A stage of a possibly asynchronous computation, that performs an action or computes a value when another CompletionStage completes. A stage completes upon termination of its computation, but this may in turn trigger other dependent stages.

What is the difference between future and CompletableFuture?

Future vs CompletableFuture. CompletableFuture is an extension to Java's Future API which was introduced in Java 5. A Future is used as a reference to the result of an asynchronous computation.

What is a CompletableFuture?

What is CompletableFuture? A CompltableFuture is used for asynchronous programming. Asynchronous programming means writing non-blocking code. It runs a task on a separate thread than the main application thread and notifies the main thread about its progress, completion or failure.

Why do we need CompletableFuture?

CompletableFuture is a Future . It overrides methods of future, meaning that you can wait for the result of the future, with or without a timeout. You can request the status of the future (whether it's done), etc. Waits if necessary for this future to complete, and then returns its result.


Video Answer


2 Answers

CompletionStage<T> is an interface of which CompletableFuture<T> is the only current implementing class. By looking at the javadoc for CompletionStage<T>, you'll notice it provides methods for taking one CompletionStage<T> and transforming it into another CompletionStage<T>. However, the returned values by the CompletionStage<T> are actually themselves CompletabeFuture<T> objects.

So using CompletabeFuture<T> is kind of the same thing as using a CompletionStage<T> but the latter can be used as the base interface for possible new classes in the future as well as being a target type for many descending types just as we tend to do List<Integer> integerList = new ArrayList<>(); rather than ArrayList<Integer> integerList = new ArrayList<>();

like image 94
Ousmane D. Avatar answered Oct 17 '22 03:10

Ousmane D.


A CompletableFuture is a CompletionStage. However, as its name suggests, it is

  • completable: It can be completed using complete or completeExceptionally.
  • a Future: You can use get method, etc. to get the result.

IMHO, in most APIs, like in your example, you should use CompletionStage, because

  • The implementation usually provides the mechanism to complete the stage. You don't need/want to expose methods like complete to the caller.
  • The caller is expected to use the returned value in an async manner instead of using blocking calls like get provided by Future.
like image 36
Xiao Avatar answered Oct 17 '22 03:10

Xiao