这个问题的前一个版本没有说到点子上,所以我试着把它归结为:
Future的map函数接受一个 ExecutionContext,如下所示(取自API 2.10.3)
def map[S](f: (T) ⇒ S)(implicit executor: ExecutionContext): Future[S]
我想传递我自己的 ExecutionContext,但我没有成功。好像我的语法不正确。
如何将我的 ExecutionContext 传递给 Future 的映射函数?
我得到的是:
val executorService = Executors.newFixedThreadPool(9)
val executionContext = ExecutionContext.fromExecutorService(executorService)
def myFunction(mt : MyType): Unit = println("my function")
def getSomeFuture()(executor: ExecutionContext): Future[MyType] = {..}
// function is served by my execution context. A Future is returned.
val f = getSomeFuture()(executionContext)
// compiles if I provide the standard global execution context - sth. I do NOT want
f map (myFunction)
// does not compile
f map (myFunction)(executionContext)
// does not compile
f map (item => println("sth."))(executionContext)