未来和承诺之间有什么区别? [英] What's the difference between a Future and a Promise?

查看:108
本文介绍了未来和承诺之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Future Promise 之间的区别是什么?

它们都像占位符一样对于未来的结果,但主要区别在哪里?

What's the difference between Future and Promise?
They both act like a placeholder for future results, but where is the main difference?

推荐答案

根据此讨论 Promise 最终被称为 CompletableFuture 包含在Java 8中,以及其javadoc 解释:

According to this discussion, Promise has finally been called CompletableFuture for inclusion in Java 8, and its javadoc explains:


可以明确完成的未来(设置其值和状态),并可用作CompletionStage ,支持在完成时触发的依赖功能和动作。

A Future that may be explicitly completed (setting its value and status), and may be used as a CompletionStage, supporting dependent functions and actions that trigger upon its completion.

列表中也给出了一个例子:

An example is also given on the list:

f.then((s -> aStringFunction(s)).thenAsync(s -> ...);

请注意,最终的API略有不同,但允许类似的异步执行:

Note that the final API is slightly different but allows similar asynchronous execution:

CompletableFuture<String> f = ...;
f.thenApply(this::modifyString).thenAccept(System.out::println);

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

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