我知道 Scala Future 变得更好的很多原因。是否有任何理由改用 Twitter Future?除了事实 Finagle 使用它。
最佳答案
免责声明:我在 Twitter 上工作过 Future 实现。一点上下文,我们在 Scala 有一个“好的”实现 Future
之前就开始了我们自己的实现。 .
这是 Twitter Future
的功能:
Future
不同在同伴中有一些新的辅助方法。 例如举个例子:
Future.join(f1, f2)
可以在异构 Future 类型上工作。Future.join(
Future.value(new Object), Future.value(1)
).map {
case (o: Object, i: Int) => println(o, i)
}
o
和 i
保留它们的类型,它们不会被转换为最不常见的父类(super class)型 Any
.例如。:
f.onSuccess {
println(1) // #1
} onSuccess {
println(2) // #2
}
#1 保证在 #2 之前执行
例如。:
val f1 = new Promise[Int]
f1.map(_ * 2).map(_ + 1)
f1.setValue(2) // <- this thread also executes *2 and +1
class MyMessage extends Exception
val p = new Promise[Int]
p.setInterruptHandler {
case ex: MyMessage => println("Receive MyMessage")
}
val f = p.map(_ + 1).map(_ * 2)
f.raise(new MyMessage) // print "Receive MyMessage"
关于scala - Twitter Future 与 Scala Future 相比有哪些优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32987855/