What is execution context in Scala?

淺唱寂寞╮ 提交于 2020-12-01 09:23:47

问题


I am new to Scala, and was trying to use some parallel constructs(Future in particular).

I found there is an implicit parameter of type ExecutionContext. IMO, it is something similar to(and maybe more abstract than) the concept of thread pool. I have tried to learn it through documentation, but I cannot find any clear and detailed introduction about it.

Could anyone please explain what exactly execution context is in Scala? And what is the purpose of introducing execution context to the language?


回答1:


The basic idea is pretty simple: you have a callback that's going to be executed at some point. On what thread will it be executed? The current one? A new one? One from a pool? That's for the execution context to decide. The default one (ExecutionContext.global) uses threads from a global pool (with a number of threads determined by how many CPU cores you have).

In other circumstances, you might want to use a different context. For example, Akka actors can use their dispatcher as an execution context.



来源:https://stackoverflow.com/questions/39939177/what-is-execution-context-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!