Java – how does akka implement the relationship before JMM?

In the official akka document, they refused:

For details, please refer to the doc

I wonder how akka did it I just passed the source code (recently at this moment), and I think there should be a lock before executing the actor Receive, but I didn't find any locks (I think) Finally, I found actorcell Comment from invoke:

yes, Mail@R_862_2419 @. status, I think this is what I'm looking for I've seen them use unsafe to access / update the status field, but I'm not sure how to ensure memory visibility

Solution

There are two things to consider: passing the message and correctly publishing the actor's internal status

The former is implemented through the messagequeue implementation of the mailbox, which will use volatile writing (default concurrentlinkedqueue) or locking (for normal linkedblockingqueue) to ensure the safe release of queued projects The actor will synchronize with the sender by reading the same volatile field (in the first case) or taking the same lock (in the second case), so all writes before the message is sent occur before anything in the actor before the message is processed

The internal state of the actor is safely stowed, even if the mailbox state you found is rearranged on different threads: after processing a batch of messages (defined by the throughput parameter), the mailbox is set to the "unscheduled" state, where is a volatile write (actually unsafe. Compareandsetint(), which has the same semantics) Before the actor starts processing messages, it uses unsafe Getintvolatile (synchronized with previous writes) to read the mailbox status, so in the last batch of messages, all writes completed by the actor will occur before all reads in this batch

You can read more about the semantics of the operations involved here, remember, sun misc. The * volatile method on unsafe follows the same rules as atomic * reference

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>