8

I have some synchronous calls in my Scala code. I've wrapped them in a blocking() context and then in a Future: Future(blocking(syncCall())), but I don't know which type of ExecutionContext to use.

I know there may be a lot of possibilities and that there is not an ExecutionContext that it's the "best". I just need some information in order not to choose the worst because there is a lot of information out there and I have a mess in my head.

vicaba
  • 2,836
  • 1
  • 27
  • 45
  • 2
    Why are you wrapping the call in a `blocking` context if you're running this inside a `Future`. Why not `Future(syncCall())`? – Yuval Itzchakov Jun 24 '16 at 14:17
  • 3
    @YuvalItzchakov to notify the ExecutionContext that it's a blocking call, read https://github.com/alexandru/scala-best-practices/blob/master/sections/4-concurrency-parallelism.md#44-must-use-scalas-blockcontext-on-blocking-io – vicaba Jun 24 '16 at 14:25
  • Good read, thanks. Following that section (4.6) says: *Related to Rule 4.4, if you're doing a lot of blocking I/O (e.g. a lot of calls to JDBC), it's better to create a second thread-pool / execution context and execute all blocking calls on that, leaving the application's thread-pool to deal with CPU-bound stuff.*. Why not use that advice? – Yuval Itzchakov Jun 24 '16 at 14:28
  • @YuvalItzchakov that is my intention and this is why I'm asking for some help on that, because I don't know which thread pool to use. Also, 4.6 does not invalidate rule 4.4 – vicaba Jun 24 '16 at 14:40
  • 1
    Scala's `ExecutionContext` doesn't do any thread scheduling on its own, but rather passes the job to the underlying `java.util.concurrent.ExecutorService`. So your question boils down to the following: which type of `ExecutorService` should one use for IO tasks. – Haspemulator Jun 24 '16 at 14:52
  • I would suggest go with section `4.11` in the reading above. Introduce few system properties where you can define your thread-pool: maxPool size, initialPool size etc. – vvg Jun 24 '16 at 16:23

0 Answers0