[英]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然而,正如它的名字所暗示的那样,
complete
or completeExceptionally
. complete
或completeExceptionally
。Future
: You can use get
method, etc. to get the result. Future
:您可以使用get
方法等来获取结果。 IMHO, in most APIs, like in your example, you should use CompletionStage
, because恕我直言,在大多数 API 中,就像在您的示例中一样,您应该使用
CompletionStage
,因为
complete
to the caller.complete
之类的方法。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.