未来与承诺的区别 [英] Difference between Future and Promise

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

问题描述

未来 promise之间有什么区别?

它们都像预留位置

根据 .edu / pipermail / concurrency-interest / 2012-December / 010423.html>此讨论 Promise 终于被调用 CompletableFuture 以包含在Java 8中,以及其javadoc 解释:


未来可以明确完成(设置其值和状态),并可用作完成区,支持完成后触发的依赖函数和动作。


列表中也列出了一个示例:

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

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

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


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

解决方案

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

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

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天全站免登陆