点赞java - 同步儿童演员
Akka java - Synchronized Children Actor
我刚开始使用 Akka(在 Java),我遇到了一个不知道如何解决的问题。
我有N个玩家和一个神谕。
创建后,所有玩家必须与神谕沟通,神谕必须回应"winner / loser"。
问题是我不知道如何依次、整齐地、一次一个地传达玩家的策略:
例如:玩家 1 -> 玩家 2 -> 玩家 3 -> 玩家 1 -> 玩家 2 -> ...
玩家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());
}
}
}
甲骨文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());
}
}
}
通过排除竞争来有序地与玩家沟通的最佳策略是什么?
感谢大家。
有序地向参与者发送消息的两个基本选项:
1. 玩家作为routies的循环路由器
2. 持有对玩家的引用并遍历他们的数组或集合
不相关的注释:
避免演员选择。通过 constructor/props 或消息将 oracle 引用传递给玩家。选择很少是合理的(主要是为了远程处理),这是一种危险的做法。
我刚开始使用 Akka(在 Java),我遇到了一个不知道如何解决的问题。
我有N个玩家和一个神谕。 创建后,所有玩家必须与神谕沟通,神谕必须回应"winner / loser"。
问题是我不知道如何依次、整齐地、一次一个地传达玩家的策略:
例如:玩家 1 -> 玩家 2 -> 玩家 3 -> 玩家 1 -> 玩家 2 -> ...
玩家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());
}
}
}
甲骨文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());
}
}
}
通过排除竞争来有序地与玩家沟通的最佳策略是什么?
感谢大家。
有序地向参与者发送消息的两个基本选项:
1. 玩家作为routies的循环路由器
2. 持有对玩家的引用并遍历他们的数组或集合
不相关的注释:
避免演员选择。通过 constructor/props 或消息将 oracle 引用传递给玩家。选择很少是合理的(主要是为了远程处理),这是一种危险的做法。