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) } }
source share