// 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)
(global one),那么 in 会在大约 10 秒内结束(但通常不会)。
~17249 lines printed
ERROR: java.util.concurrent.TimeoutException: Futures timed out after [10 seconds]
took: 10.646309398 seconds
这就是问题所在:为什么使用固定的前上下文池它会在没有消息传递的情况下停止,但对于全局前上下文它会终止但有错误和消息传递?
有时..它是不可重现的。
更新:我确实看到了"ERROR"
,"took"
如果我将池从 1 增加到 N。不管 N 有多高 - 它仍然会是错误。
代码在这里:https ://github.com/Sergey80/scala-samples/tree/master/src/main/scala/concurrency/apptmpl