How Scala Futures Scheduled in Multicore

How does Scala determine when to execute the future immediately, on another processor (core), and when its turn to free the current core? Or, if it uses a completely different method, please explain: how are Scala futures planned? (Through processors, through threads, through time)

+3


source to share


1 answer


It will be based on the ExecutionContext that Future uses. When the future starts a task or calls a callback, it uses the context to define the policy.

Note that in a typical Scala application they are passed as implicits, so you may not always see them. Ex. in playback mode

import scala.concurrent.ExecutionContext.Implicits.global

      

This is often used to create a default context that actually comes from Akka.

You can specify your own thread pool like this:



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

      

And use it like this (or implicit)

Future {do something}(ctx)

      

So, to answer your question, the streaming policy is just what you say (which is often implied to you)

+1


source







All Articles