Java - synchronized ArrayList still ConcurrentModificationException

Stefan xyz :

I got an final ArrayList<RoutingTableEntry> routingTable = new ArrayList<>(); which is accessed multiple times. But I only get at one Point an ConcurrentModificationException which is in the following Thread:

Thread checkReplies = new Thread(() -> {

    while (true) {

        synchronized (routingTable) {

            for (RoutingTableEntry entry : routingTable) { // throws it here

                // do smth
            }
        }

        // [...]
    }
 });
 checkReplies.start();

It throws the exception at the loop even though the routingTable is already synchronized. This thread gets only executed once per class.

Any ideas?

Gabriele Mariotti :

Starting from your comment in the original question, you are removing items in the routingTable while you are iterating.

You can't do it (also if the routingTable is synchronized)

for (RoutingTableEntry entry : routingTable) { // throws it here
     // routingTable.remove(....);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=142295&siteId=1