Scala 中的执行上下文是什么? [英] What is execution context in Scala?

查看:57
本文介绍了Scala 中的执行上下文是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Scala 的新手,并试图使用一些并行结构(特别是 Future).

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

我发现有一个 ExecutionContext 类型的隐式参数.IMO,它类似于(并且可能比)线程池的概念.我试图通过文档来学习它,但我找不到任何清楚详细的介绍.

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.

谁能解释一下Scala中的执行上下文到底是什么?将执行上下文引入语言的目的是什么?

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

推荐答案

基本思想非常简单:您有一个将在某个时刻执行的回调.它将在哪个线程上执行?现在的?一个新的?一个从游泳池?这是由执行上下文来决定的.默认的 (ExecutionContext.global) 使用全局池中的线程(线程数由您拥有的 CPU 内核数决定).

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).

在其他情况下,您可能需要使用不同的上下文.例如,Akka actor 可以使用他们的调度程序作为执行上下文.

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

这篇关于Scala 中的执行上下文是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆