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

前端 未结 3 600
盖世英雄少女心
盖世英雄少女心 2020-12-30 21:34

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, S

3条回答
  •  生来不讨喜
    2020-12-30 22:18

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

    So using CompletabeFuture is kind of the same thing as using a CompletionStage 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 integerList = new ArrayList<>(); rather than ArrayList integerList = new ArrayList<>();

    You can read the post introduction to CompletionStage and CompletableFuture for more details.

提交回复
热议问题