Scala期货和Java 8 CompletableFuture [英] Scala Futures and java 8 CompletableFuture

查看:67
本文介绍了Scala期货和Java 8 CompletableFuture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 8中CompletableFutures的引入带来了scala.concurrent.Future中可用的语言功能,例如单数转换.

The introduction of CompletableFutures in Java 8 brought to the language features available in the scala.concurrent.Future such as monadic transformations.

  • 有什么区别,为什么Scala开发人员应该优先使用Scala Futures而不是Java 8 CompletableFuture?

在Java中通过akka.dispatch网桥使用scala.concurrent.Future还是有充分的理由吗?

Are there still good reasons to use the scala.concurrent.Futurein Java through akka.dispatch bridge?

推荐答案

有什么区别?为什么Scala开发人员应该选择Scala Futures而不是java 8 CompletableFuture?

What are the differences, and why a Scala developer should prefer Scala Futures over java 8 CompletableFuture ?

重述@ dk14在注释中指出的内容,我想说CompletableFuture没有惯用的Scala api.

Rephrasing what @dk14 pointed out in comments I'd say that CompletableFuture doesn't have idiomatic Scala api.

对于scala开发人员来说,含义是:

For scala developer the implications are:

  • 缺乏理解力,因为它不遵循单子类型常见的Scala方法约定
  • 使用大部分方法时需要的
  • java标量互操作开销
  • lack of for comprehensions due to fact that it does not follow Scala method conventions common for monadic types
  • java-scala interop overhead needed when using big part of it's methods

还值得注意的是,Java CompletableFuture与scala Future并不完全等效.它是scala FuturePromise的融合.

It is also worth noting that java CompletableFuture is not exactly equivalent of scala Future. It is rather a fuse of scala Future and Promise.

考虑到上面列出的缺点,除非在设计应该与Java无缝互操作的公共api,否则在scala中使用CompletableFuture并没有多大意义.

Considering the cons listed above there isn't much sense in using CompletableFuture in scala unless you are designing public api that should be seamlessly interoperable with java.

是否仍然有充分的理由通过akka.dispatch桥在Java中使用scala.concurrent.Future?

Are there still good reasons to use the scala.concurrent.Future in Java through akka.dispatch bridge?

我特别在寻找在Java中使用akka.dispatch的原因,如果仍有的话

I am particularly looking for reasons to use akka.dispatch in Java, if there are still any

Akka是在scala之上构建的,有时会使用scala Future.这意味着,如果您有一部分用Java编写的代码,则值得将其包装在scala api中(使用akka.dispatch java api),以便能够在akka上轻松使用它.

Akka is build on top of scala and it sometimes uses scala Futures. This means that in cases when you have some portion of code written in java it is worth to wrap it in scala api (with akka.dispatch java api) to be able to easily use it with akka.

例如,您正在Java中实现akka actor.处理消息时,您想要进行一些非阻塞的读取,读取完成后,应该将结果作为消息发送给另一个参与者.

For example, you are implementing akka actor in java. When processing message you want to do some non-blocking reading that, when done, should produce result as a message to another actor.

您可以做的是将I/O放入Java 可调用,然后使用

What you could do is to put your I/O into java Callable, then use akka.dispatch.Futures#future to get scala Future out of it, and then you could leverage akka pipe to make result of the future be delivered as a message to some actor.

这篇关于Scala期货和Java 8 CompletableFuture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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