Java: java.util.ConcurrentModificationException

核能气质少年 提交于 2019-12-23 17:27:21

问题


I am making a 2D and currently working on shooting with bullets. The bullet is a seperate class. All the bullets are stored in an arraylist called bullets. I am trying to make it destroy itself when it is out of the side of the screen (< -16), but when I try it gives me this error.

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at GameCanvas.updateMovement(GameCanvas.java:94)
at GameCanvas.gameLoop(GameCanvas.java:63)
at GameCanvas.<init>(GameCanvas.java:28)
at GameClient.<init>(GameClient.java:68)
at GameClient.main(GameClient.java:29)

I assume it has something do with when it is being destroy, I am using this code to destroy it:

public void move() {

    if(x > -16) {
        x -= move_speed;
    } else {
        kill();
    }

}

public void kill() {
    ObjectHandler.bullets.remove(this);
}

updateMovement() method:

public void updateMovement() {
    PlayerObject.update();

    for(Bullet bullet : ObjectHandler.bullets) {
        bullet.move();


    }

}

Why is it doing this? Thanks.


回答1:


This happens because you are modifying a list while you're iterating over it.

Try to "remember" what you need to kill and after you've passed the whole list, go through your "memory" and perform the appropriate kills. This way you will modify the list after iterating over it.

E.g.:

public void move() {

    if(x > -16) {
        x -= move_speed;
    } else {
        ObjectHandler.remember_to_kill(this);
    }

}

// in ObjectHandler:

private HashSet<type_of_object_to_kill> kill_memory = new...;

public void remember_to_kill() {
    this.kill_memory.add(this);
}

private void kill_from_memory() {
    for (type_of_object_to_kill obj: kill_memory) {
        ObjectHandler.bullets.remove(this);
    }
    ObjectHandler.kill_memory.clear();
}

// update movement:

public void updateMovement() {
    PlayerObject.update();

    for(Bullet bullet : ObjectHandler.bullets) {
        bullet.move();
    }
    ObjectHandler.kill_from_memory();
}



回答2:


From the stack trace we can see you have a method that gameLoop() that calls updateMovement() in GameCanvas. The latter iterates over bullets in main thread. At the same time your kill() method modifies the list. This is exactly what the exception says and prevents you from further damage.

You have to synchronize access to bullets list or use thread-safe collection like CopyOnWriteArrayList.




回答3:


This is usually thrown when you are iterating through a list and trying to remove/modify an item from the list at the same time. I'm not sure exactly what is going on. More code examples would be helpful.




回答4:


The problem is that you're trying to modify the collection removing an element while you're reading at the same time. Of course this might be dangerous and so the exception is thrown. If you use an iterator you could leverage its functions to remove the current element.

Something like this:

public void updateMovement() {
    PlayerObject.update();

    Iterator<Bullet> iterator = ObjectHandler.bullets.iterator();
    while(iterator.hasNext()) {
        Bullet bullet = iterator.next();
        if(x > -16) {
            x -= move_speed;
        } else {
            iterator.remove();
        }
    }
}


来源:https://stackoverflow.com/questions/9437139/java-java-util-concurrentmodificationexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!