1
我正在学习和理解Akka演员模型以及一些基本示例。Akka演员在处理第一条消息之前正在处理第二条消息
我为同一个Actor(即“helloworld1”和“helloworld2”)创建了两个实例并向该Actor发送消息。第一条消息由实例“helloworld1”发送,第二条消息由“helloworld2”发送。
当我运行下面的代码,我得到的输出如下
Yes you made it Hi helloworld2
Yes you made it Hi helloworld1
我的问题是为什么这是第二个消息在第一处理?我期待下面的输出,因为第一条消息是由helloworld1
Yes you made it Hi helloworld1
Yes you made it Hi helloworld2
代码ActorApp发送:
import akka.actor.{ActorSystem, Props}
object ActorApp {
def main(args :Array[String]) ={
val system = ActorSystem("SimpleActorSystem")
val actorObj1 = system.actorOf(Props[HelloWorld],"helloworld1")
actorObj1 ! "Hi helloworld1"//first message sent
val actorObj2 =system.actorOf(Props[HelloWorld],"helloworld2")
actorObj2 ! "Hi helloworld2"//second message sent
}
}
代码演员:
import akka.actor.Actor
class HelloWorld extends Actor {
def receive = {
case dataStr:String => println("Yes you made it "+dataStr)
}
}
消息的顺序是在同一个演员内部保证的,而不是在演员之间 –