How are akka actors implemented on underlying threads?

前端 未结 3 1230
悲&欢浪女
悲&欢浪女 2021-02-01 23:02

Given an execution context and a thread pool, how are akka/scala actors scheduled/implemented on that?

3条回答
  •  不思量自难忘°
    2021-02-01 23:28

    I was confused about this topic for a long time. I assumed that there is some relation between threads and actors. For each actor, there is a thread that hosts it, so I was thinking. Probably there are several actors for each thread that work in cooperative multitasking mode.

    Documentation is focused on usage and covers internal architecture lightly. You just should extend the Actor class and you would get a working actor. So I tried to guess how can this be done and imagined that each Actor has life-cycle, something like wait asynchronously for a message queue, then process message and then go to start.

    That was a totally false assumption. Although the documentation says "actor life-cycle" it does not mean Actor life-cycle. Actor mentioned is a conception rather than an actual object implementing the Actor class. The object resides passively in the java heap.

    To breathe life into the conception, coordinated work involving a bunch of real workers is needed. And the heart of it is a dispatcher. It holds all actors, threads, and messages. And when the resources become available and there are messages available for processing the dispatcher activates. It takes an appropriate Actor object, wraps its receive method in runnable, and passes it to a spare thread. So there is no life-cycle for the object only occasional method calls from the actor system.

    When you think of it, it makes much more sense than the scheme I assumed earlier. But it was hard for me to deduce it from available documentation. It was written by seasoned concurrency programmers. The distinguish holding the actor class from actor conception behind it. They know that dispatcher for an actor system is like a task scheduler for an OS. So they quickly get to the point and describe various flavors and realization of the conception assumed to be familiar to everyone.

    But that is not so easy for a newbie. He has no acquaintance of the dispatcher pattern in the actor systems context. I tried to google "dispatcher pattern" and it showed definitions non-relevant to the actor system.

    I've found easily double dispatch, multi-dispatch, and other OOP topics. I've found something similar to Akka's routers message dispatcher. There is probably a decent description for actor message dispatchers but it not so easy to find.

    I hope I've cleared the misunderstanding.

提交回复
热议问题