1

I don't get how I can specify an certain execution service from Java, like a thread pool, to execute my callback of a future. For example, I can have a chain of map operations:

val executionService = java.util.concurrent.Executors.newSingleThreadExecutor()
val twitterExecutor = com.twitter.util.FuturePool.apply(executionService)
val p = com.twitter.util.Promise.apply[Int]
val r = p.map(t => t + 1).map(t => t + 1).map(t => t + 1)

// Do something else ...

// This could also happen from another thread:
p.updateIfEmpty(Return(10))

com.twitter.util.Await.ready(p)

How can I specify the initial execution service twitterExecutor to be used for the callbacks and how can I change the execution service for one single map callback? In the Scala standard library I can specify the implicit execution context. I don't want to create a future which is already completed but complete it later via the promise.

I have only found this question: https://groups.google.com/forum/#!topic/finaglers/ovDL2UFKoDw where the answer suggests using flatMap but I want to avoid returning a new future in every callback.

and this explanation: https://twitter.github.io/finagle/guide/Futures.html but it shows me how to create a new future from a pool which I want to avoid since I am starting with a promise.

4

0 回答 0