Data sharing between threads in robocode
This is my first post to this grup and I have a little problem with
sharing a static object between threads. There is two thread class and
each of them refer to a static object of a third class through static
methods of the same class.
Problem is, when they refer to that static member, I realised that they
don't refer to same object although I declared it static.
// This is the third class that I mentioned above
public class Repository {
public static ArrayList<String> enemyNames = new
ArrayList<String>();
public static HashMap<String, EnemyBot> enemies = new
HashMap<String, EnemyBot>();
public static HashMap<String, TeamBot> teammates = new
HashMap<String, TeamBot>();
public Repository() {
}
public static synchronized void
updateEnemyRepository(ScannedRobotEvent event, TeamRobot myRobot)
{
....
}
public class WarTorch extends TeamRobot
{
public void run()
{
while(true)
{
setTurnRadarRight(Integer.MAX_VALUE);
execute();
}
}
public void onScannedRobot(ScannedRobotEvent e)
{
// Here this class refer to the third class.
Repository.updateEnemyRepository(e, this);
for (Iterator it = Repository.enemyNames.iterator();
it.hasNext();) {
Object key = (String) it.next();
System.out.println(key + " - "
+Repository.enemies.get((String)key).getRobotName() + ", " +
Repository.enemies.get((String)key).getCoordinates());
System.out.println(e.getName());
}
}