javaconcurrencyfuturepromise

What's the difference between a Future and a Promise?


What's the difference between Future and Promise?
They both act like a placeholder for future results, but where is the main difference?


Solution

  • According to this discussion, Promise has finally been called CompletableFuture for inclusion in Java 8, and its javadoc explains:

    A Future that may be explicitly completed (setting its value and status), and may be used as a CompletionStage, supporting dependent functions and actions that trigger upon its completion.

    An example is also given on the list:

    f.then((s -> aStringFunction(s)).thenAsync(s -> ...);
    

    Note that the final API is slightly different but allows similar asynchronous execution:

    CompletableFuture<String> f = ...;
    f.thenApply(this::modifyString).thenAccept(System.out::println);