“CompletionStage"和“CompletableFuture"有什么区别 [英] What is the difference between 'CompletionStage' and 'CompletableFuture'
问题描述
我在他们每个人中都看到了一个例子,但我需要确切地知道 deep 的区别是什么,因为有时我认为我可以同时使用它们来获得相同的结果,所以我想知道以便我可以选择正确的?
分别使用它们有什么好处?
就像这个例子一样:
public CompletionStagegetNextQueryUUID() {返回 CompletableFuture.supplyAsync(() -> {String nextId = dbRequestService.getNextRequestQueryUUID();返回 ok(nextId);}, 执行者);}public CompletableFuturegetNextQueryUUID() {返回 CompletableFuture.supplyAsync(() -> {String nextId = dbRequestService.getNextRequestQueryUUID();返回 ok(nextId);}, 执行者);}
<块引用>
这个例子在Play framework
中运行.
CompletionStage
是一个接口,其中 CompletableFuture
是当前唯一的实现类.通过查看 CompletionStage
的 javadoc,您会注意到它提供了用于获取一个 CompletionStage
并将其转换为另一个 CompletionStage
CompletionStage
返回的值实际上是 CompletabeFuture
对象.
因此,使用 CompletabeFuture
与使用 CompletionStage
有点类似,但后者可以用作可能的新类的基本接口将来以及作为许多降序类型的目标类型,就像我们倾向于做的那样 List
而不是 ArrayList
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
.
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<>();
这篇关于“CompletionStage"和“CompletableFuture"有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!