【发布时间】:2017-05-05 23:26:15
【问题描述】:
我在 Scala 中使用 akka 演员。我想知道是否有办法让演员在处理收到的消息时可以定期检查其邮箱中是否有其他消息,并且跟随这些消息可以改变其变量。
类似的方案:
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 试图强制执行的设计模式,所以我不鼓励这样做。您可以通过实现自己的队列来放置消息,并使用其他一些机制来检查该队列,从而构建类似的东西。
-
这是不可能的。参与者一次处理一条消息。这就是它们在并发环境中保证安全可变状态的方式。