开发者

What are the weaknesses in using Immutability + Actor model for concurrency programming?

开发者 https://www.devze.com 2023-04-12 21:06 出处:网络
While building a large multi threaded application for the financia开发者_如何学JAVAl services industry, I utilized immutable classes and an Actor model for workflow everywhere I could. I\'m pretty ple

While building a large multi threaded application for the financia开发者_如何学JAVAl services industry, I utilized immutable classes and an Actor model for workflow everywhere I could. I'm pretty pleased with the outcome. It uses a fair amount of heap space (its in Java btw) but the JVM's GC works pretty well with short lived immutable classes.

I am just wondering if there are any downsides to using this kind of pattern going forward? When debugging a team mates code, I often find myself recomending this pattern in one way or another. I guess once one has a hammer, everything looks like a nail. So the question is: When would this design pattern (paradigm?) work poorly?

My hunch is when memory usage is a big issue or when the project restrictions require something along the lines of low-level C, etc.


Many science simulations codes are really memory intensive. For example for cellular automata models fast memory access is more important than CPU power. In that case, accessing and modifying in place a mutable array is always faster (at least in all my trials).


All depends of your project design.

If you have some resource and lot of actors use it then the common pattern is to design accessor actor. Then when some other actor needs to ask about some resource, he ask about it accessor actor. Then the answer is copied through message channel.

Now imagine - you have really heavy resource (eg map[String, BigObject]) and other actors frequently ask about some BigObject then you waste your bandwidth.
Better idea would be to share the resource to all actors in readonly mode, and make one actor to perform writes.

Other example would be database connector which connect to database with a lot of blob data. When database connector is thread safe (as normally is) it's better to share the connector object reference to all actors, then design some actor which provides the access.

All you need to remember every that communication between actors is done by copying messages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号