'CompletionStage'和'CompletableFuture'有什么区别 [英] What is the difference between 'CompletionStage' and 'CompletableFuture'

查看:555
本文介绍了'CompletionStage'和'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?

使用它们中的每一个有什么好处?

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);
}

此示例在Play framework中运行.

推荐答案

CompletionStage<T>

CompletionStage<T> is an interface which CompletabeFuture<T> is the only implementing class of this interface. By looking at the java doc for CompletionStage<T>, you'll notice the CompletionStage interface provides methods for taking one CompletionStage and transforming it into another CompletionStage. However, these objects being returned by the CompletionStages as actually themselves CompletabeFuture<T> objects.

因此使用CompletabeFuture<T>与使用CompletionStage<T>是一样的事情,但是后者可以用作将来可能的新类的基本接口,并且可以用作许多降序类型的目标类型我们倾向于做List<Integer> integerList = new ArrayList<>();而不是ArrayList<Integer> integerList = new ArrayList<>();

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简介更多细节.

这篇关于'CompletionStage'和'CompletableFuture'有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆