Scala - futures不运行 [英] Scala - futures does not run
本文介绍了Scala - futures不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图运行以下未来的基本代码
I am trying to run the following future basic code
future { println("ssss")} onSuccess{ case _ => println("succ")}
$ b $ p
然而,当我运行main方法时,打印,系统几乎立即退出。我使用隐式ExecutionContext。任何提示?
However, when I run the main method, nothing to the console is printed and the system exits almost instantly. I am using the implicit ExecutionContext. Any hints?
此代码:
val f = future(Await.ready(Promise().future, d.timeLeft))
f.onSuccess {
case _ => println("hee")
}
>
推荐答案
期货在专用线程池上执行。如果你的主程序不等待未来,它将立即退出,未来将没有机会执行。
这里可以做的是在主程序中使用 Await
阻塞主线程,直到将来执行:
Futures are executed on a dedicated thread pool. If your main program does not wait for the future, it will exit immediately and the future won't have a chance to execute.
What you can do here is to use Await
in your main program to block the main thread until the future executes:
def main( args: Array[String] ) {
val fut = future { println("ssss")}
fut onSuccess{ case _ => println("succ")}
Await.result( fut )
}
这篇关于Scala - futures不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文