I\'m modelling a game where multiple players (threads) move at the same time. The information of where a player is located at the moment is stored twice: the player has a variab
You should not feel bad about your modelling - this is only a two way navigable association.
If you take care (as in the other answers told) to manipulate atomic, e.g. in the Field methods, thats fine.
public class Field {
private Object lock = new Object();
public removePlayer(Player p) {
synchronized ( lock) {
players.remove(p);
p.setField(null);
}
}
public addPlayer(Player p) {
synchronized ( lock) {
players.add(p);
p.setField(this);
}
}
}
It would be fine if "Player.setField" were protected.
If you need further atomicity for "move" semantics, go one level up for board.