Scala Future和Java Future之间有什么区别 [英] What are the differences between a Scala Future and a Java Future

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

问题描述

Scala Future和Java Future之间是否存在任何概念,功能或机械差异?从概念上讲,我看不出任何差异,因为它们都旨在提供异步计算机制。

Are there any conceptual, functional or mechanical differences between a Scala Future and a Java Future? Conceptually I can't see any differences as they both aim to provide a mechanism for asynchronous computation.

推荐答案

主要的不便之处 java.util.concurrent.Future 是你无法在不阻止的情况下获得价值的事实。

The main inconvenience of java.util.concurrent.Future is the fact that you can't get the value without blocking.

实际上,检索值的唯一方法是 get 方法,(从文档中引用

In fact, the only way to retrieve a value is the get method, that (quoting from the docs)


等待if计算完成所必需的,然后检索其结果。

Waits if necessary for the computation to complete, and then retrieves its result.

使用 scala.concurrent.Future 你得到一个真正的非阻塞计算,因为你可以附加回调以完成(成功/失败)或者只是映射它并以多种方式将多个期货连在一起。

With scala.concurrent.Future you get instead a real non-blocking computation, as you can attach callbacks for completion (success/failure) or simply map over it and chain multiple Futures together in a monadic fashion.

长话短说,scala的Future允许异步计算wi阻止超过必要的线程。

Long story short, scala's Future allows for asynchronous computation without blocking more threads than necessary.

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

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