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
A trivial solution would be
synchronized(player) {
synchronized(field) {
// code
}
}
However, make sure that you always lock the resources in the same order to avoid deadlocks.
Note that in practice, the bottleneck is the field, so a single lock on the field (or on a dedicated, common lock object, as @ripper234 rightly pointed out) may suffice (unless you are concurrently manipulating players in other, conflicting ways).