Akka Future - Parallel versus Concurrent?

别来无恙 提交于 2019-12-21 05:08:19

问题


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?


回答1:


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 flatMaped (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)
      }
  }


来源:https://stackoverflow.com/questions/35849001/akka-future-parallel-versus-concurrent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!