简体   繁体   English

如何通过 java 中的循环在 AKKA 中创建多个 Actor?

[英]How to create multiple Actors in AKKA by a loop in java?

this is just a learning example, I wan't to create and assign a actor for each element in an array,这只是一个学习示例,我不想为数组中的每个元素创建和分配一个演员,

ActorSystem system = ActorSystem.create("mySystem");
for(String name:namesArray){
  ActorRef myActor = system.actorOf(Props.create(Actor.class),name);
}

Will this create multiple actor or will it create actor paths that reference to the same actor?这会创建多个演员还是会创建引用同一演员的演员路径? if its the latter how do i create multiple actors and assign them a identifier?如果是后者,我如何创建多个参与者并为他们分配一个标识符?

That code will:该代码将:

  • Create an ActorSystem创建一个ActorSystem
  • Create an actor for every name in the namesArray (it will throw an InvalidActorNameException if a name appears a second time in the array)namesArray中的每个名字创建一个 actor(如果一个名字在数组中第二次出现,它将抛出InvalidActorNameException

Note that this code as written promptly forgets the ActorRef for every one of the created actors, meaning that you would not be able to send it a message except by using ActorSelection which is generally not recommended.请注意,编写的这段代码会立即忘记每个创建的 Actor 的ActorRef ,这意味着您将无法向它发送消息,除非使用通常不推荐的ActorSelection Going along with this, it's worth noting that the actor will continue running even if there are no ActorRef s for that actor (actors must be explicitly stopped).与此同时,值得注意的是,即使该演员没有ActorRef (必须明确停止演员),该演员仍将继续运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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