问题描述
来自精心编写的
该图指出,我理解 c $ c> numSummer 和 charConcat
将在同一线程上运行。
是是否可以并行运行每个 Future
,即在单独的线程上运行?
左边的图片是它们并行运行的。
插图的重点是 Future .apply
方法是启动执行的方法,因此,如果直到第一个future的结果被 flatMap
ed(如图所示)之后才执行
(请注意,启动是指相关的 ExecutionContext
被告知工作。 it 如何并行化是一个不同的问题,可能取决于诸如其线程池的大小。)
左侧的等效代码:
val numSummer =未来{...} //开始执行
val charConcat =未来{...} //执行开始
numSummer.flatMap {numsum =>
charConcat.map {string =>
(数字,字符串)
}
}
右边:
未来{...} //开始执行
.flatMap {numsum =>
Future {...} //执行开始(注意,只有在
//第一个Future的结果( numsum)可用之前,这种情况才会发生。)
.map {string =>
(数字,字符串)
}
}
From the well-written Akka Concurrency:
As I understand, the diagram points out, both numSummer
and charConcat
will run on the same thread.
Is it possible to run each Future
in parallel, i.e. on separate threads?
The picture on the left is them running in parallel.
The point of the illustration is that the Future.apply
method is what kicks off the execution, so if it doesn't happen until the first future's result is flatMap
ed (as in the picture on the right), then you don't get the parallel execution.
(Note that by "kicked off", i mean the relevant ExecutionContext
is told about the job. How it parallelizes is a different question and may depend on things like the size of its thread pool.)
Equivalent code for the left:
val numSummer = Future { ... } // execution kicked off
val charConcat = Future { ... } // execution kicked off
numSummer.flatMap { numsum =>
charConcat.map { string =>
(numsum, string)
}
}
and for the right:
Future { ... } // execution kicked off
.flatMap { numsum =>
Future { ... } // execution kicked off (Note that this does not happen until
// the first future's result (`numsum`) is available.)
.map { string =>
(numsum, string)
}
}
这篇关于Akka Future-并行还是并行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!