简体   繁体   English

“CompletionStage”和“CompletableFuture”有什么区别

[英]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?我在他们每个人中都看到了一个例子,但我需要确切地知道 deep 有什么不同,因为有时我认为我可以同时使用它们来获得相同的结果,所以我想知道以便我可以选择正确的一?

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 .此示例在Play framework运行。

CompletionStage<T> is an interface of which CompletableFuture<T> is the only current implementing class. CompletionStage<T>是一个接口,其中CompletableFuture<T>是当前唯一的实现类。 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> .通过查看CompletionStage<T>的 javadoc,您会注意到它提供了用于获取一个CompletionStage<T>并将其转换为另一个CompletionStage<T> However, the returned values by the CompletionStage<T> are actually themselves CompletabeFuture<T> objects.但是, CompletionStage<T>返回的值实际上是CompletabeFuture<T>对象。

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<>();因此,使用CompletabeFuture<T>与使用CompletionStage<T>有点相同,但后者可以用作将来可能的新类的基本接口,以及作为许多降序类型的目标类型,就像我们一样倾向于做List<Integer> integerList = new ArrayList<>(); rather than ArrayList<Integer> integerList = new ArrayList<>();而不是ArrayList<Integer> integerList = new ArrayList<>();

A CompletableFuture is a CompletionStage . CompletableFuture是一个CompletionStage However, as its name suggests, it is然而,正如它的名字所暗示的那样,

  • completable: It can be completed using complete or completeExceptionally . completable:可以使用completecompleteExceptionally
  • a Future : You can use get method, etc. to get the result. a Future :您可以使用get方法等来获取结果。

IMHO, in most APIs, like in your example, you should use CompletionStage , because恕我直言,在大多数 API 中,就像在您的示例中一样,您应该使用CompletionStage ,因为

  • The implementation usually provides the mechanism to complete the stage.实现通常提供完成阶段的机制。 You don't need/want to expose methods like complete to the caller.您不需要/不想向调用者公开诸如complete之类的方法。
  • The caller is expected to use the returned value in an async manner instead of using blocking calls like get provided by Future .调用者应以异步方式使用返回值,而不是使用像Future提供的get类的阻塞调用。

One is an interface and the other is a class.一个是接口,另一个是类。 Usually you return the interface and not the implementation, but I doubt this is the case here.通常你返回接口而不是实现,但我怀疑这里的情况。 Returning CompletableFuture makes more sense for me.返回CompletableFuture对我来说更有意义。

Unless you are using some other implementation of that interface of course, like Spring's DelegatingCompletableFuture , but from your examples you are not.除非您当然使用该接口的其他一些实现,例如 Spring 的DelegatingCompletableFuture ,但从您的示例来看,您不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CompletionStage,CompletableFuture Void - 返回什么? - CompletionStage, CompletableFuture Void - what to return? Java CompletableFuture 的 thenApply 和 thenApplyAsync 有什么区别? - What is the difference between thenApply and thenApplyAsync of Java CompletableFuture? 使用CompletionStage而不是CompletableFuture - using CompletionStage instead of CompletableFuture Java CompletionStage/CompletableFuture 苦苦挣扎 - Struggling with Java CompletionStage/CompletableFuture 并行流和CompletableFuture之间的差异 - Difference between parallel stream and CompletableFuture 返回 void 与返回 CompletionStage 之间有什么区别<Void> ? - Is there any difference between returning a void vs returning a CompletionStage<Void>? 如何在异常处理中使用CompletableFuture和CompletionStage - How to use CompletableFuture and CompletionStage with exception handling CompletableFuture、Future 和 RxJava 的 Observable 的区别 - Difference between CompletableFuture, Future and RxJava's Observable Vert.x 3,请求处理程序POST,异步CompletionStage或CompletableFuture - Vert.x 3, request handler POST, async CompletionStage or CompletableFuture java completableFuture 是否有方法返回 CompletionStage<u>来处理异常?</u> - Does java completableFuture has method returning CompletionStage<U> to handle exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM