How is it possible to get a ConcurrentModificationException for this code-block?
synchronized (list) {
for (Iterator<?> it = list.iterator(); it.hasNext(); ) {
Object object = it.next();
// do something to object without touching list
}
}
Edit: sorry, that was not specific enough: // do something to object does not touch the list
By having another thread modifying the list, for example.
Just because you synchronize this code block doesn’t mean all the accesses to the list are blocked. It only makes sure that all the other blocks synchronized on the list can’t execute in parallel.
If the list is shared by several threads, all its usages must be synchronized on the same monitor.