Akka java

编程入门 行业动态 更新时间:2024-10-18 16:48:24
Akka java - 同步儿童演员(Akka java - Synchronized Children Actor)

我刚开始使用Akka(Java),我遇到了一个我不知道如何解决的问题。

我有N个玩家和一个神谕。 一旦创建,所有玩家必须与必须回应“赢家/输家”的神谕交流。

问题是,我不知道依次整齐地,一次一个地传达玩家的策略: 例如:player1 - > player2 - > player3 - > player1 - > player 2 - > ...

玩家类:

public class MyPlayers extends UntypedActor { /* * I created one context of N Player */ public MyPlayers() { } public void preStart() { //in this way all N players at the beginning, start to communicate concurrently. ActorSelection oracle = getContext().actorSelection("//Main/user/app/oracle"); oracle.tell(new MyMessage(myValue), getSelf()); } @Override public void onReceive(Object msg) throws Exception { if (msg instanceof MyMessage) { //do something sender().tell(MyMessage, getSelf()); } if (msg instanceof DoneMsg) { if (((DoneMsg) msg).isDone()) { log("Stop Players"); getContext().stop(getSelf()); } } }

Oracle类:

public class Oracle extends UntypedActor { private int nPlayers; public Oracle(int nPlayers) { this.nPlayers = nPlayers; } public void preStart() { stateGame = false; //When the play starter, there is not any wins. } @Override public void onReceive(Object msg) throws Exception { if (msg instanceof MyMessage) { if (!stateGame) { //do something sender().tell(MyMessage, getSelf()); } else { //do something getContext().stop(getSelf()); } } }

排除竞争,以有序的方式沟通球员的最佳策略是什么?

谢谢大家。

I just started with Akka (in Java) and I have a problem that I do not know how to solve.

I have N players and one oracle. Once created, all players must communicate with the oracle who must respond "winner / loser".

The problem is that I do not know the strategy to communicate the players in turn, tidily, one at a time: for example: player1 -> player2 -> player3 -> player1 -> player 2 -> ...

Players class:

public class MyPlayers extends UntypedActor { /* * I created one context of N Player */ public MyPlayers() { } public void preStart() { //in this way all N players at the beginning, start to communicate concurrently. ActorSelection oracle = getContext().actorSelection("//Main/user/app/oracle"); oracle.tell(new MyMessage(myValue), getSelf()); } @Override public void onReceive(Object msg) throws Exception { if (msg instanceof MyMessage) { //do something sender().tell(MyMessage, getSelf()); } if (msg instanceof DoneMsg) { if (((DoneMsg) msg).isDone()) { log("Stop Players"); getContext().stop(getSelf()); } } }

Oracle class:

public class Oracle extends UntypedActor { private int nPlayers; public Oracle(int nPlayers) { this.nPlayers = nPlayers; } public void preStart() { stateGame = false; //When the play starter, there is not any wins. } @Override public void onReceive(Object msg) throws Exception { if (msg instanceof MyMessage) { if (!stateGame) { //do something sender().tell(MyMessage, getSelf()); } else { //do something getContext().stop(getSelf()); } } }

what is the best strategy to communicate the players in an orderly manner by excluding competition?

Thanks to all.

最满意答案

您有条不紊地向演员发送消息的基本两个选项: 1.循环路由器,其中玩家是路线 2.包含对玩家的引用并迭代它们的数组或集合

无关的说明: 避免演员选择。 通过构造函数/道具或消息将oracle引用传递给玩家。 选择很少是合理的(主要是用于远程处理),这是一种危险的做法。

Your basic two options to send messages to actors in an orderly manner: 1. Round robin router where players are routies 2. Array or collection holding references to players and iterating through them

Unrelated notes: Avoid actor selection. Pass the oracle reference to the players by their constructor/props or a message. Selection is very rarely justified (mostly just for remoting) and it's a dangerous practice.

更多推荐

本文发布于:2023-04-29 03:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334511.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Akka   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!