问题
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.
A scheme like:
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
}
}
}
回答1:
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.
来源:https://stackoverflow.com/questions/41247523/how-to-periodically-check-an-actor-mailbox-and-alter-variables-in-scala