score:0

Accepted answer

Untested and mostly guessed. I have never used Scala actors

I think it's an implementation detail. To me it's unclear when the body of the function passed to the actor function is executed.

It seems that it regards the mailbox as empty as long as you have not defined a recieve function. The recieve function is executed asynchronously, so I would recommend you to check the mailbox from within that function.

val firstActor = actor {
  receive {
    case x => 
      println("First actor received: " + x)
      println("Messages in mailbox: " + mailboxSize)
  }

  Thread.sleep(3000)
  println("Messages in mailbox: " + mailboxSize)
}

for(i <- 1 to 7)
{
  firstActor ! "Hello" + i
  println("Message sent")
}

I guess that the above code would print something similar to this:

Message sent
Message sent
Message sent
Message sent
First actor received: Hello2
Messages in mailbox: 4
Message sent
Message sent
First actor received: Hello1
Messages in mailbox: 6
...
Messages in mailbox: 6

Note that the output may be quite different as I can not predict in what order and at what speed the actor system handles messages.

As other said in comments, when you work with actors, the recommended library is Akka


Related Query

More Query from same tag