java.util.ConcurrentModificationException in Android Game Loop -


i using canvas, moving objects on screen, when object hit left side of canvas (x=0), object of same type gets instantiated, , start moving on screen.

everything works fine, few objects gets created , start moving around screen.

at point, receive concurrent modification exception in run method game loop is, gameobjs arraylist:

@override public void run() {      while(isrunning){         if(!myholder.getsurface().isvalid())             continue;         canvas canvas = myholder.lockcanvas();         canvas.drawrect(0,0,canvas.getwidth(), canvas.getheight(), pwhite);           for(mygameobject gameobj : gameobjs){             gameobj.move(canvas);         }          myholder.unlockcanvasandpost(canvas);     }  } 

i've tried use iterator, still getting same error.

i appreciate help. thank in advance!

collections.synchronizedlist(...) won't work if happening... (throws concurrentmodificationexception...)

public class concurrenttest {      public static void main(string[] args) {         list<string> things = new arraylist<>();          runnable modifythread = () -> {             while(true) {                 for(int k = 0; k < 1000; k++) {                     things.add(string.valueof(k));                 }                  while(!things.isempty()) {                     things.remove(0);                 }             }         };          runnable readthread = () -> {             while(true) {                 for(string thing : collections.synchronizedlist(things)) {                     system.out.println(thing);                 }             }         };          new thread(modifythread).start();         new thread(readthread).start();     } } 

try , find other places in code list being modified. there must thread manipulating collection while iterating through it.

you take copy of list before iterating on it.

for example, in code above, try...

for(string thing : new arraylist<>(things)) { 

...instead of...

for(string thing : collections.synchronizedlist(things)) { 

...and no longer exception (but still won't "correct", if run it, see loads of nulls being printed out...)

even better, keep read loop as...

for(string thing : things) { 

...but change type of list...

list<string> things = new copyonwritearraylist<>(); 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -