未来与承诺的区别

编程入门 行业动态 更新时间:2024-10-23 09:29:19
本文介绍了未来与承诺的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

未来和 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);

更多推荐

未来与承诺的区别

本文发布于:2023-11-25 02:46:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   未来

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!