I have a hashmap used in multiple threads at the same time. To make it thread safe I put it into a synchronized block:
private final Map<Long, DeviceConnection> mapConnections = new HashMap()<>;
...
synchronized (mapConnections) {
List<Long> toClear = new ArrayList<>();
for (Map.Entry<Long, AndroidSocketConnection> entry : mapConnections.entrySet()) {
if (entry.getValue().isReadyToRemove())) {
removed++;
toClear.add(entry.getKey());
}
}
for(Long toC : toClear) {
mapConnections.remove(toC);
}
}
I thought if I put it into synchronized block I do not have to care about such stuff, but this Exception is thrown:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1442)
at java.util.HashMap$EntryIterator.next(HashMap.java:1476)
at java.util.HashMap$EntryIterator.next(HashMap.java:1474)
at myPackage.network.DeviceHandler.doClearing(DeviceHandler.java:51) // -> this line contains the for loop head of the code I showed
at java.lang.Thread.run(Thread.java:748)