java “CompletionStage”和“CompletableFuture”有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47571117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between 'CompletionStage' and 'CompletableFuture'
提问by Ebraheem Alrabee'
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
.
采纳答案by Ousmane D.
CompletionStage<T>
is an interface of which CompletabeFuture<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.
CompletionStage<T>
是一个接口,它CompletabeFuture<T>
是当前唯一的实现类。通过查看 的 javadoc CompletionStage<T>
,您会注意到它提供了获取一个CompletionStage<T>
并将其转换为另一个 的方法CompletionStage<T>
。但是, 返回的值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<>();
rather than ArrayList<Integer> integerList = new ArrayList<>();
因此 usingCompletabeFuture<T>
与使用 a 有点相同,CompletionStage<T>
但后者可以用作未来可能的新类的基本接口,也可以作为许多降序类型的目标类型,就像我们倾向于做的那样,List<Integer> integerList = new ArrayList<>();
而不是ArrayList<Integer> integerList = new ArrayList<>();
You can read the post introduction to CompletionStage and CompletableFuturefor more details.
您可以阅读CompletionStage 和 CompletableFuture的帖子介绍以了解更多详细信息。
回答by Xiao
A CompletableFuture
is a CompletionStage
. However, as its name suggests, it is
ACompletableFuture
是一个CompletionStage
。然而,正如它的名字所暗示的那样
- completable: It can be completed using
complete
orcompleteExceptionally
. - a
Future
: You can useget
method, etc. to get the result.
- 可完成:可以使用
complete
或 完成completeExceptionally
。 - 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. - The caller is expected to use the returned value in an async manner instead of using blocking calls like
get
provided byFuture
.
- 实现通常提供完成阶段的机制。您不需要/不想向
complete
调用者公开方法。 - 调用方应以异步方式使用返回值,而不是使用
get
由 提供的阻塞调用Future
。
回答by Eugene
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
对我来说更有意义。
Unlessyou are using some other implementation of that interface of course, like Spring's DelegatingCompletableFuture
, but from your examples you are not.
除非您当然使用该接口的其他一些实现,例如 Spring 的DelegatingCompletableFuture
,但从您的示例来看,您不是。