Which ExecutionContext to choose for the future in Akka applications?

When fulfilling the Future, we need a context of fulfillment.

Afaik, we can use the following solution:

1.

import scala.concurrent.ExecutionContext.Implicits.global

2.

import context.dispatcher

or

implicit val ec = context.system.dispatcher

3.

implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))

My question is: what is the difference between the two? Which way to suggest?

eg. Solution2 will reuse the actor manager, while solution1 will use a separate fixed pool, the thread number will be the same as the cpu core. I just know some fragment of the difference, can anyone explain more? Thank!

+4
source share
2 answers

Apparently, you are using futures inside the actor, as you mention context.dispatcherand context.system.dispatcher. Note that these two parameters are not necessarily the same (but they are usually the same):

  • context.system.dispatcher - .
  • context.dispatcher - , , .

ExecutionContext , :

, , (, ), context.dispatcher.

, "" , context.dispatcher (, , ), , , , . , - :

implicit val executionContext: ExecutionContext =
  context.system.dispatchers.lookup("my-blocking-dispatcher")

my-blocking-dispatcher application.conf. :

my-blocking-dispatcher {
  type = Dispatcher
  executor = "thread-pool-executor"
  thread-pool-executor {
    fixed-pool-size = 10
  }
  throughput = 1
}

, - Akka. ,

implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))

, , context.dispatcher . , , .

+5

Future - . Thread, .

context.dispatcher, akka. , . Future context.dispatcher, Future , . , (!) . , , , .

akka (dispatchers) . . , , ( , akka ). ( )

akka .

0

Source: https://habr.com/ru/post/1694084/


All Articles