无法Promise.complete
只能运行Future.onComplete
的回调,而不能完全通过ExecutionContext
,并且据我了解,安排Future.onComplete
的稍后回调并可能在另一个线程中运行它?
答案 0 :(得分:4)
您可以提供自己的ExecutionContext
到onComplete
,它将在同一个帖子上运行代码:
val immediateContext: ExecutionContext = new ExecutionContext {
def execute(runnable: Runnable) {
runnable.run()
}
def reportFailure(cause: Throwable) {}
}
您甚至可以将其设为implicit
,并且对于您希望在另一个线程中执行执行的情况,您可以提供scala.concurrent.ExecutionContext.global
或其他一些上下文。
这是一个测试,它是如何工作的:
val immediateContext: ExecutionContext = new ExecutionContext {
override def reportFailure(cause: Throwable): Unit = {}
override def execute(runnable: Runnable): Unit = {
println("Executing")
runnable.run()
println("Executed")
}
}
def testContext(): Unit = {
println("Scheduling on an uncompleted future")
val p = Promise[Int]()
println("Scheduling")
p.future.onComplete { _ => println("Completed") }(immediateContext)
println("Scheduled")
p.complete(Success(5))
println()
println("Scheduling on an already completed future")
val p2 = Promise[Int]().complete(Success(5))
println("Scheduling")
p2.future.map { n =>
println("Mapping")
n * 2
}(immediateContext).onComplete{
case Success(n) => println(s"Completed: $n")
case _ =>
}(immediateContext)
println("Scheduled")
println()
println("Using scala.concurrent.ExecutionContext.global for comparison")
val p3 = Promise[Int]().complete(Success(5))
println("Scheduling")
p3.future.onComplete {
_ => println("Completed")
}(concurrent.ExecutionContext.global)
println("Scheduled")
}
正在运行testContext()
将打印
Scheduling on an uncompleted future
Scheduling
Scheduled
Executing
Completed
Executed
Scheduling on an already completed future
Scheduling
Executing
Mapping
Executed
Executing
Completed: 10
Executed
Scheduled
Using scala.concurrent.ExecutionContext.global for comparison
Scheduling
Scheduled
Completed
答案 1 :(得分:1)
这是实现scala Futures的设计决策,所有操作(map,flatMap等)都需要隐式ExecutionContext。
如果你想在线程之间有更好的线程可重用性和更少的上下文切换,我建议你看看scalaz Task,对于延迟计算略有不同的抽象:http://timperrett.com/2014/07/20/scalaz-task-the-missing-documentation/