简体   繁体   English

Scala 中的执行上下文是什么?

[英]What is execution context in Scala?

I am new to Scala, and was trying to use some parallel constructs( Future in particular).我是 Scala 的新手,并试图使用一些并行构造(尤其是Future )。

I found there is an implicit parameter of type ExecutionContext .我发现有一个ExecutionContext类型的隐式参数。 IMO, it is something similar to(and maybe more abstract than) the concept of thread pool. IMO,它类似于(并且可能比)线程池的概念。 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?谁能解释一下Scala中的执行上下文究竟是什么? And what is the purpose of introducing execution context to the language?将执行上下文引入语言的目的是什么?

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).默认的 ( ExecutionContext.global ) 使用全局池中的线程(线程数由您拥有的 CPU 内核数决定)。

In other circumstances, you might want to use a different context.在其他情况下,您可能希望使用不同的上下文。 For example, Akka actors can use their dispatcher as an execution context.例如,Akka actor 可以使用他们的调度程序作为执行上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM