为什么akka确实需要不可变的消息 [英] Why exactly does akka need immutable messages

查看:169
本文介绍了为什么akka确实需要不可变的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或换句话说:是否正确使用了可变消息?

or put another way: are there proper uses of mutable messages?

我面临的用例是我想要处理基本上类型为<的对象/ p>

The use case I am facing is that I want to process objects which basically are of type

Map<String,List<String>>

演员所做的典型处理是读取或写入地图的某些字段。一个简洁的例子是(省略 null -tests等。)

The typical processing an actor would do is to read or write some of the fields of the map. A condensed example is (leaving out null-tests etc.)

  map.get("somekey").add("new value");

我的预感是,通过使用相应的集合类型,在Scala中保持这种不可变性是微不足道的。在Java中,它需要一些额外的类库。

My hunch is that keeping this immutable in Scala would be trivial by using the respective collection types. In Java it would require to go for some additional class library.

但是:阅读Akka文档,我发现发送邮件介绍了发件人的最后一次访问与第一次访问接收演员。因此,如果 map 不是不可变的,那么发件人应该看到所有数据。

But: reading the Akka docs, I find that sending a message introduces a happens-before relation between the last access of the sender and the first access of the receiving actor. So if the map is not immutable, nevertheless the sender should see all data.

假设我可以确保一旦发送,发送方将永远不会再触摸地图,是否有任何其他问题需要关于对该地图的线程数据访问?

Suppose I can make sure that the sender will never touch the map again once it is send, is there any other problem to expect with regards to threaded data access to this map?

推荐答案

OP对先发条件规则的解释是正确的:演员发送规则意味着在向A处理M之前发送M给演员A (这是相同指的)。

The OP’s interpretation of the happens-before rule is correct: the "actor send rule" means that sending M to actor A happens-before processing M by A (this is what "the same" refers to).

回答主要问题:只要最多只有一个演员可以拥有任何可变地图在某个时间点,这将起作用,并且根据具体情况,这可能是解决问题的最有效方法。但是,保证单一所有权将需要一些纪律,这会使维护成本抵消运行时优势。

To answer the main question: as long as only at most one actor can "own" the mutable map at any given point in time, this will work, and depending on the circumstances this may well be the most efficient solution to the problem. Guaranteeing the single-ownership will require a bit of discipline, though, which offsets the runtime advantage by a maintenance cost.

虽然原始问题遗漏了实际的运输工具 Map 我想强调Randall的观点,即演员之间的消息永远不应该是JDK类型,因为那些缺乏语义含义。在这种情况下, Map 应包含在明确命名的自定义消息类型中。

Although the original question leaves out the actual vehicle for transporting the Map I would like to reinforce Randall’s point that messages between actors should never be JDK types, since those lack semantic meaning. The Map should in this case be contained within a clearly named custom message type.

这篇关于为什么akka确实需要不可变的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆