java - Major Concurrency Issue -


i have looked around cannot find decent answer issue having. have list being modified , read keep getting concurrency issues. note: there 2 threads pull data list. issues pop on doentitytick() , getentity(). note: doentitytick issues caused me adding entity list.

world:

package unnamedrpg.world;  import java.awt.rectangle; import java.util.arraylist; import java.util.collection; import java.util.collections; import java.util.iterator;  import unnamedrpg.player.game.gamemanager; import unnamedrpg.world.entity.entity; import unnamedrpg.world.entity.entitycharacter; import unnamedrpg.world.entity.entityliving; import unnamedrpg.world.entity.entityprojectile;   public class world {      private string name;     private arraylist<boundary> boundlist = new arraylist<boundary>();     private collection<entity> entitylist = collections.synchronizedlist(new arraylist<entity>());      public world(string name){         setname(name);     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public arraylist<entity> getentitylist(){         arraylist<entity> newlist = new arraylist<entity>();         synchronized(entitylist) {             iterator<entity> iter = entitylist.iterator();             while (iter.hasnext()) {                 entity ent = iter.next();                 newlist.add(ent);             }         }         return newlist;     }      public arraylist<entity> getentityatcoordinatepair(double x, double y){         arraylist<entity> templist = new arraylist<entity>();         for(entity ent : getentitylist()){             rectangle rect = new rectangle((int)ent.getx(), (int)ent.gety(), ent.getwidth(), ent.getheight());             if(rect.contains(x, y)){                 templist.add(ent);             }         }         return templist;     }      public void addentity(entity ent){         synchronized(entitylist){             entitylist.add(ent);             if(ent.getid() == -1){                 ent.setid(entitylist.size());             }         }     }      public entity getentity(int id){         synchronized(entitylist) {               iterator<entity> = entitylist.iterator();               while (i.hasnext()){                   entity ent = i.next();                   if(ent.getid() == id){                       return ent;                   }               }            }         return null;     }      public void doentitytick(){         synchronized(entitylist) {             iterator<entity> iter = entitylist.iterator();              while (iter.hasnext()) {                 entity ent = iter.next();                 if(ent instanceof entityliving){                     entityliving entliv = (entityliving)ent;                     if(entliv.gethealth() <= 0){                         entliv.setdead();                     }                     if(entliv.isdead() && entliv.gethealth() > 0){                         gamemanager.isspawning = true;                     }                     entliv.doentitytick();                 }                 if(ent instanceof entityprojectile){                     entityprojectile entproj = (entityprojectile)ent;                     entproj.dotick();                     entproj.setcurrentrange(entproj.getcurrentrange() + 1);                     if(entproj.getcurrentrange() >= entproj.getrange()){                         entproj.setdead();                     }                 }                  if(ent.isdead() && !(ent instanceof entitycharacter)){                     iter.remove();                 }             }         }        }      public void addbounds(boundary bounds){         boundlist.add(bounds);     }      public int getboundsid(int x, int y){         for(int i=0;i<boundlist.size();i++){             if(boundlist.get(i).contains(x, y))                 return i;         }         return -1;     }      public int getboundsid(boundary bounds){         for(int i=0;i<boundlist.size();i++){             if(boundlist.get(i) == bounds)                 return i;         }         return -1;     }      public void removebounds(boundary bounds){         boundlist.remove(bounds);     }      public boundary getbounds(int x, int y){         for(boundary bounds : boundlist){             if(bounds.contains(x, y)){                 return bounds;             }         }         return null;     }      public boolean isinbounds(int posx, int posy){         for(boundary bounds : boundlist){             if(bounds.contains(posx, posy)){                 return true;             }         }         return false;     }  } 

stack trace:

exception in thread "thread-1" java.util.concurrentmodificationexception     @ java.util.arraylist$itr.checkforcomodification(unknown source)     @ java.util.arraylist$itr.next(unknown source)     @ unnamedrpg.world.world.doentitytick(world.java:83)     @ unnamedrpg.player.game.gamemanager$1.run(gamemanager.java:49) 

so after entire day of looking @ code managed find fix. instead of having entities spawn instead stick them queue , await next world tick in order spawn them. thank attempted , have given +1 have helped me come across answer.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -