Can someone tell me why the code below throws a ConcurrentModificationException (at least under Java 6)? It was my understanding that because the code takes a lock on the synchronized list prior to iterating the remove inside that block should not be allowed but when I run this it is (and the print out shows 2 items in the list) and the next iteration then throws a ConcurrentModificationException.
There are two flavours, one using the for loop and one with an iterator. Both produce the same result. I am scratching my head here. I would expected a deadlock which itself is not desirable but at this stage I am just confused about the remove being allowed.
Joined: Jan 23, 2013
Just answered my own question. Because its executing within the synchronized block.