【发布时间】:2015-09-12 23:41:27
【问题描述】:
// 1 fixed thread
implicit val waitingCtx = scala.concurrent.ExecutionContext.fromExecutor(Executors.newFixedThreadPool(1))
// "map" will use waitingCtx
val ss = (1 to 1000).map {n => // if I change it to 10 000 program will be stopped at some point, like locking forever
service1.doServiceStuff(s"service ${n}").map{s =>
service1.doServiceStuff(s"service2 ${n}")
}
}
每个doServiceStuff(name:String) 需要 5 秒。 doServiceStuff 没有隐式 ex:Execution 上下文作为参数,它在内部使用自己的 ex 上下文并在其上执行Future {blocking { .. }}。
最后程序打印:
took: 5.775849753 seconds for 1000 x 2 stuffs
如果我将 1000 更改为 10000,添加更多任务:val ss = (1 to 10000) 然后程序停止:
~17 027 行将被打印(共 20 000 行)。没有“错误”消息 将被打印。不会打印“接受”消息
**并且不会再进行任何处理。
但如果我将 exContext 更改为 ExecutionContext.fromExecutor(null: Executor)(全局变量),那么 in 会在大约 10 秒后结束(但不是正常情况)。
~17249 lines printed
ERROR: java.util.concurrent.TimeoutException: Futures timed out after [10 seconds]
took: 10.646309398 seconds
这就是问题 : 为什么使用固定的前上下文池它会停止没有消息传递,但使用全局前上下文它会终止但有错误和消息?
有时..它是不可重现的。
更新:如果我将池从 1 增加到 N,我确实会看到 "ERROR" 和 "took"。不管 N 有多高 - 它仍然会是错误。
代码在这里:https://github.com/Sergey80/scala-samples/tree/master/src/main/scala/concurrency/apptmpl
【问题讨论】:
标签: scala concurrent.futures executioncontext