简体   繁体   中英

Sequence of arrival of messages in the actor mail box when using ask and tell

I have confusion on what is the sequence of messages arrived in the mailbox in the following code:

class myAct extends Actor {
def receive = {
  case 1 =>
    println(1)
  case 2 =>
    sender ! 90
  case 3 =>
    println(3)
}

}

In the driver I am sending messages to the actor

myActor ! 1
myActor.ask(2).mapTo[Int].onComplete {
case Success(x) =>
  println(x)
case Failure(ex) =>
  println(ex)}
myActor ! 3

The question is in what order will the messages be delivered to the mail box. Will 1 always arrive first. will 3 always arrive last. Is there a possibility of 2 arriving before 1.

There is a specific section of the documentation on Message Ordering . If your "driver" is another Actor then the ordering is clear. From the documentation:

The rule more specifically is that for a given pair of actors, messages sent directly from the first to the second will not be received out-of-order. The word directly emphasizes that this guarantee only applies when sending with the tell operator to the final destination, not when employing mediators or other message dissemination features (unless stated otherwise).

If the driver is not inside of an Actor then the default implicit sender is Actor.noSender . Per Konrad's comment it seems that the ordering is still preserved in this case as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM