How to determine the number of actors to spawn in akka?

爷,独闯天下 提交于 2019-12-05 04:30:03

问题


I have recently started looking into the Akka 2.0 framework and was able to get some code running, spawning actors that perform simple oracle database calls, performing simple calculations and whatnot, nothing in production however.

What I want to know, is there a general rule of thumb or best practice to determining how many actors to spawn for certain types of tasks? Say for example, I have a connection pool of 200 jdbc connections, Do I create an actor to represent each connection? Do I create a handful of them and use a round-robin approach?

Thanks.


回答1:


Note that numberOf(actors) != numberOf(threads).

You should create an actor for every entity that would otherwise share mutable state across threads. The whole thing about the actor model is that it shall isolate mutable state so that only immutable messages get exchanged between the actors. The result is that you don't need any locks anymore and you can easily reason about the thread safety of your program because all mutable state is isolated in actors and you can rely on the framework to properly pass the memory barrier whenever required, e.g. when switching an actor from one thread to another.

The number of threads is a different subject: This depends on the number of cores and the blocking coefficient for each thread, i.e. the percentage of time it spends waiting for other threads or the I/O subsystem. For example, if your actors are doing CPU intensive calculations (e.g. calculating Pi) then the blocking coefficient will be close to 0%. If however your actors are doing mostly I/O, you can easily assume a blocking coefficient of 90% or more.

Finally, the number of threads can be calculated like this:

int threads = Runtime.getRuntime().availableProcessors() * 100 / (100 - blockingCoefficient)

where blockingCoefficient represents an integer percentage between 0 and 99 inclusively.




回答2:


You can create as many actors as you like, however, you're limited to about 2 billion per parent, also don't forget to stop them when they are done. Also, do not create your actors as top level unless they're actually top-level actors. (i.e. create actors inside actors using context.actorOf instead of system.actorOf)



来源:https://stackoverflow.com/questions/10879296/how-to-determine-the-number-of-actors-to-spawn-in-akka

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!