我知道Akka調度程序與全局執行上下文之間的基本區別。Akka執行上下文與未來全局上下文
我嘗試這個代碼與scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global
val resultF = (0 to 100).map(
x =>
Future {
val startTime = System.currentTimeMillis()
val task = Future {
Thread.sleep(100)
x
}
task.onSuccess {
case result =>
val timeRemaining = System.currentTimeMillis() - startTime
println(s"$result $timeRemaining")
}
}
)
StdIn.readLine()
上述代碼打印上的平均值)的時間等於了Thread.sleep(,周圍103毫秒的平均。
但是下面的代碼打印100-400毫秒之間的時間。
val system = ActorSystem("test")
implicit val executionContext = system.dispatcher
val resultF = (0 to 100).map(
x =>
Future {
val startTime = System.currentTimeMillis()
val task = Future {
Thread.sleep(100)
x
}
task.onSuccess {
case result =>
val timeRemaining = System.currentTimeMillis() - startTime
println(s"$result $timeRemaining")
}
}
)
StdIn.readLine()
我不理解什麼是除了使用thread-pool
的主要區別。
沒有更精確的問題... http://doc.akka.io/docs/akka/2.5/scala/dispatchers.html – cchantep
@cchantep我很瞭解調度員,但我不是肯定爲什麼在上面兩個代碼片段中有這麼多差異。 – Atiq