20

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?

3
  • 1
  • 1
    Think of an execution context as an interpreter of asynchronous instructions (Futures), which decides when and how long to run them. If you'd like more explanation, see Viktor Klang's great talk about it youtu.be/K9lt6yjuzbM
    – Yawar
    Oct 10, 2016 at 2:52
  • @engineerC , Thanks, actually that is the link I put in my question. But I think it does not really clearly explain the concept.
    – Lifu Huang
    Oct 10, 2016 at 3:39

1 Answer 1

31

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.