简体   繁体   English

Scala的期货如何在多核中安排

[英]How are Scala's futures scheduled across multicores

How does Scala determine when to execute a future immediately, on a different CPU (core), and when to queue it for the current core to free? Scala如何确定何时立即执行未来,在不同的CPU(核心)上,以及何时将其排队以使当前核心免费? Or, if it uses a completely different method, please explain: How are Scala Futures scheduled? 或者,如果它使用完全不同的方法,请解释:Scala期货如何安排? (Across CPUs, across Threads, across time) (跨越CPU,跨线程,跨时间)

It would be based on the ExecutionContext that the Future uses. 它将基于Future使用的ExecutionContext。 When the future runs the task, or invokes the callback it uses the context to decide the policy. 当将来运行任务或调用回调时,它使用上下文来决定策略。

Note that in a typical Scala app these are passed as implicits so you may not always see them. 请注意,在典型的Scala应用程序中,这些作为隐含传递,因此您可能无法始终看到它们。 Ex. 防爆。 in Play 在Play中

import scala.concurrent.ExecutionContext.Implicits.global

That's often used to bring in a default context, which actually comes from Akka. 这通常用于引入默认上下文,实际上来自Akka。

You can specify your own thread pool like this: 您可以像这样指定自己的线程池:

  val ctx = ExecutionContext.fromExecutor(
  Executors.newFixedThreadPool(5))

And use it like this (or an implicit) 并使用它(或隐含的)

Future {do something}(ctx)

So to answer your question the threading policy is simply what you tell it (which is often an implicit made available to you) 因此,要回答您的问题,线程策略就是您所说的(通常是隐含的,可供您使用)

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

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