Search code examples
akkaactor

How does Akka decide which Actor gets the Thread?


Say we have three Akka Actors, A, B and C running on a dispatcher with only one Thread and the following happens:

  1. A receives a message and starts processing it
  2. In the meantime a message is sent to B and simultaneously a message is sent to C. Since there are no Threads available, both B and C put these messages in their mailbox
  3. A now finishes processing its message and has no more messages in its mailbox, so releases the Thread back into the pool
  4. B or C now both need this Thread. Are there any guarantees for which will be put on the Thread first?
  • How does Akka make this decision? Does it round robin on all Actors in the ActorSystem?
  • Is this decision configurable?
    • Can I say prioritise Actor C to get Threads before Actor B in these situations?

Solution

  • The whole reason why to use Akka is to not have to deal with this sort of thing. You do not want (or need) to prioritize actors in a way like this. Internal dispatcher logic is complex and very well optimized to process tasks as fast possible. Prioritization should be done through other means - like routers, or priority mailboxes.

    Now to answer your question: the default dispatcher is backed by a blocking queue - the actor who first received the message in his mailbox will be selected first.