如何定期检查actor邮箱并更改Scala中的变量 [英] How to periodically check an actor mailbox and alter variables in scala
问题描述
我在Scala中使用akka演员。我想知道是否有一种方法可以让一个参与者,在处理收到的消息时可以定期检查其邮箱中是否有其他消息,而跟随此消息可以更改其变量。
I am using akka actors in Scala. I would like to know if there is a way to have an actor, which while processing a message received can periodically check its mailbox for other messages and following this messages can alter its variables.
类似这样的方案:
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
}
}
}
推荐答案
没有什么可以说,您不能使用Actor在一个Future中将长时间运行的任务移到另一个线程中。您的参与者将继续响应邮箱中的消息,而另一个线程在您的处理中中断。您可以在将来设置回调,然后让任务正常完成。
Nothing says you can't move a long running task into a different thread, using a Future, inside an actor. Your actor would continue to respond to messages in the mailbox, while another thread chugs away on your processing. You can set up your callbacks on the future, and let the task finish normally.
这篇关于如何定期检查actor邮箱并更改Scala中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!