I have an object that is singleton. This object declares:
List<Player> players = new ArrayList<Player>();
The same object also indicates 4 operations on this array list:
public List<Player> getPlayers() { return players; } // the result of this method can be used in another object as an iterator (or maybe by index-access)
and
public void removePlayer(Player player) { players.remove(player); } public void addPlayer(Player player) { players.add(player); } public boolean isPresent(Player player) { if (players.constans(player)) {... }
Right now in the constructor, I am doing this:
players = Collections.synchronizedList(new ArrayList<Player>());
But what is the CORRECT way to synchronize these methods. It seems that if I use an iterator in another class, it will still be through a parallel modification exception. Does an exception occur if two threads simultaneously call the delete and contain methods? There are many threads to access singleton, so I would like to know how to do this, with minimal impact on performance.
source share