Scala的期货线程池有多大?
我的Scala应用程序创建了数百万future {}s,我想知道我是否可以通过配置线程池来优化它们.
谢谢.
在Scala中,我可以Await用来等待未来完成.但是,如果我已经注册了一个回调,以便在完成该未来的情况下运行,那么我怎样才能等待未来完成以及回调完成?
这是一个最小但完整的程序来说明问题:
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{ Await, Future }
object Main {
def main(args: Array[String]): Unit = {
val f: Future[Int] = Future(0)
f.onSuccess { case _ =>
Thread.sleep(10000)
println("The program waited patiently for this callback to finish.")
}
// This waits for `f` to complete but doesn't wait for the callback
// to finish running.
Await.ready(f, Duration.Inf)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
The program waited patiently for this callback to finish.
Run Code Online (Sandbox Code Playgroud)
相反,没有输出; 程序在回调结束前退出.
请注意,这与等待未来完成的问题不同,这已在此问题中得到解答.
我正在尝试运行以下未来的基本代码
future { println("ssss")} onSuccess{ case _ => println("succ")}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行main方法时,没有打印到控制台,系统几乎立即退出.我正在使用隐式ExecutionContext.任何提示?
这段代码:
val f = future(Await.ready(Promise().future, d.timeLeft))
f.onSuccess {
case _ => println("hee")
}
Run Code Online (Sandbox Code Playgroud)
也立即退出....