Scala Futures and Streams

Reading the scala source code for scala.concurrent.Future and scala.concurrent.impl.Future, it seems that every future composition via map sends a new task to the artist. I assume that this usually triggers a context switch for the current thread and / or assigning the thread for the job.

Considering that function flows should go around the Futures between them in order to influence the results of futures without blocking (or not to delve into the callback spaghetti), is this "reactive" paradigm very expensive in practice when the code is well written in a modular way, where does each function just do something small and move on to others?

+6
source share
1 answer

It depends on the execution context. So you can choose a strategy.

You, the executor, can simply do this in the calling thread, storing the card calls in one thread. You can pass your own strategy by explicitly passing the execution context or using implicit.

First I have to check what the default fork / join pool does by registering which thread was used. Newer versions of Afaik sometimes use a feed thread. However, I do not know if future callbacks were used / applied for scala.

+2
source

Source: https://habr.com/ru/post/983662/


All Articles