Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For (1) you have the guarantee that msg1 will be enqueued by the dispatcher before msg2. What actually happens once they are enqueued is really dependent on which dispatcher you use: <a href="http://akka.io/docs/akka/1.1.2/scala/dispatchers.html" rel="noreferrer">http://akka.io/docs/akka/1.1.2/scala/dispatchers.html</a>, but in your case then so long as B can accept both messages then it will always receive msg1 before msg2. </p> <p>For (2), no you do not have this guarantee. The ! method returns as soon as the dispatcher enqueues the message not when the message is accepted by the target actor's mailbox. The sending is then done in another thread and is subject to all kinds of race conditions.</p> <blockquote> <p>Is message sent by the ! operator through the network truly asynchronously or does it wait until the receiving mailbox gets it?</p> </blockquote> <p>You can use a BoundedMailbox with local actors to show that enqueuing messages to dispatchers is asynchronous with !: </p> <pre><code>class TestActor extends Actor { val mailboxCapacity = BoundedMailbox(capacity = 1) self.dispatcher = Dispatchers.newExecutorBasedEventDrivenDispatcher("test", 1, mailboxCapacity).build def receive = { case x: String =&gt; Thread.sleep(1000) println("Received message") case _ =&gt; } } val t = Actor.actorOf[TestActor] t.start() t ! "one"; t ! "two"; t ! "three"; println("Main thread"); </code></pre> <p>Prints:</p> <pre><code>scala&gt; t ! "one"; t ! "two"; t ! "three"; println("Main thread"); Received message Main thread scala&gt; Received message Received message </code></pre> <p>Which means that code execution in the main thread continues before you even know whether or not the message will ever be delivered. In this case the message send could easily have timed out if we set a pushTimeout on the dispatcher and made Thread.sleep wait for longer than the timeout. </p> <p>Compare this to using !!:</p> <pre><code>scala&gt; t !! "one"; t !! "two"; t !! "three"; println("test"); Received message Received message Received message test </code></pre> <p>So, with this in mind. The way to achieve (2) would be:</p> <pre><code>case msg1 =&gt; cRef !! msg3 aRef ! msg4 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload