Question

Error log:

[26-12-13 3:16]: java.util.ConcurrentModificationException
[26-12-13 3:16]: at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
[26-12-13 3:16]: at java.util.ArrayList$Itr.next(ArrayList.java:791)
[26-12-13 3:16]: at server.event.cycle.CycleEventHandler.stopEvents(CycleEventHandler.java:60) [26-12-13 3:16]: at server.model.players.Client.logout(Client.java:292)

Method:

public void stopEvents(Object owner) {
    for (CycleEventContainer c : events) {
        if(c.getOwner() == owner) {
            c.stop();
        }
    }
}

Question: How do I fix this error?

Edit; stop method:

public void stop() {
    isRunning = false;
    event.stop();
}

Would this work?

public void stopEvents(Object owner) {
    ArrayList<CycleEventContainer> garbageEvents = new ArrayList<>();
    for (CycleEventContainer c : events) {
        if(c.getOwner() == owner) {
            garbageEvents.add(c);
        }
    }
    for (CycleEventContainer c: garbageEvents) {
        c.stop();
    }
    garbageEvents.clear();
}
Was it helpful?

Solution

ConcurrentModificationException will be thrown every time a Collection adds or removes any element while iterating it. This is introduced to let the thread who iterates the Collection aware of the modification. So I believe your codes modify events somewhere else in other threads since your codes listed here don't seem to add or remove any element. You may check out this. Hope it helps.

OTHER TIPS

In this situation ConcurrentModificationException means that another thread is trying to modify events. You need to use synchronization or a thread-safe collection instead of ArrayList.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top