我在Scala中使用akka演员。我想知道是否有办法让演员在处理收到的消息时可以定期检查其邮箱中的其他消息,并在此消息后可以更改其变量。如何定期检查演员邮箱并更改scala中的变量
如A方案:
class Actor1 (constructors){
def receive={
case "go" => run() //the actor starts
case "alter variables" // a new message is stashed in mailbox
}
def run={
//do stuff
check(mailbox) //while the porocessing of the "go" message
// is not finished
if ("alter variables" in mailbox) {
change a variable value
}
}
}
我不知道你为什么想这样做,但我认为这会打破Akka试图强制执行的设计模式,所以我不鼓励这样做。通过实现你自己的队列,你可以通过实现自己的队列来安装类似的东西,并且有一些其他的机制来检查队列。 – childofsoong
这是不可能的。参与者一次处理*一条消息*。这是他们如何保证并发环境中安全的可变状态。 – Ryan