Som Bhattacharyya
Som Bhattacharyya

Reputation: 4112

How does multi core performance relate to execution contexts and thread pools in Scala

So i have a existing Spring library that performs some blocking tasks(exposed as services) that i intend to wrap using Scala Futures to showcase multi processor capabilities. The intention is to get people interested in the Scala/Akka tech stack.

Here is my problem. Lets say i get two services from the existing Spring library. These services perform different blocking tasks(IO,db operations). How do i make sure that these tasks(service calls) are carried out across multiple cores ? For example how do i make use of custom execution contexts? Do i need one per service call? How does the execution context(s) / thread pools relate to multi core operations ?

Appreciate any help in this understanding.

Upvotes: 1

Views: 345

Answers (1)

sebszyller
sebszyller

Reputation: 853

You cannot ensure that tasks will be executed on different cores. The workflow for the sample program would be as such.

  1. Write a program that does two things on two different threads (Futures, Java threads, Actors, you name it).
  2. JVM sees that you want two threads so it starts two JVM threads and submits them to the OS process dispatcher (or the other way round, doesn't matter).
  3. OS decides on which core to execute each thread. Usually, it will try to put threads on different cores to maximize the overall efficiency but it is not guaranteed; you might have a situation that your 10 JVM threads will be executed on one core, although this is extreme.

Rule of the thumb for writing concurrent and seemingly parallel applications is: "Here, take my e.g. 10 threads and TRY to split them among the cores."

There are some tricks, like tuning CPU affinity (low-level, very risky) or spawning a plethora of threads to make sure that they are parallelized (a lot of overhead and work for the GC). However, in general, OS is usually not that overloaded and if you create two e.g. actors, one for db one for network IO they should work well in parallel.

UPDATE: The global ExecutionContext manages the thread pool. However, you can define your own and submit runnables to it myThreadPool.submit(runnable: Runnable). Have a look at the links provided in the comment.

Upvotes: 2

Related Questions