Akka Future - Parallel or Parallel?

From the well-written Akka Concurrency :

enter image description here

As I understand it, the diagram indicates that numSummer and charConcat will work on the same thread.

Is it possible to run each Future in parallel, i.e. on separate threads?

+5
source share
1 answer

The image on the left is parallel execution.

The point of the illustration is that the Future.apply method is what starts with execution, so if this does not happen until the first result in the future is flatMap ed (as in the picture on the right), then you will not get parallel execution.

(Note that at β€œstart” I mean that the corresponding ExecutionContext told about the task. How it is parallelized is another question and may depend on things like the size of the thread pool.)

Equivalent code for left:

 val numSummer = Future { ... } // execution kicked off val charConcat = Future { ... } // execution kicked off numSummer.flatMap { numsum => charConcat.map { string => (numsum, string) } } 

and on the right:

 Future { ... } // execution kicked off .flatMap { numsum => Future { ... } // execution kicked off (Note that this does not happen until // the first future result (`numsum`) is available.) .map { string => (numsum, string) } } 
+11
source

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


All Articles