One small comment: a ConcurrentModificationException does not only occur when two threads access the same collection. It may also happen that your own (single-threaded) code iterates over a collection, and makes changes to it during the iteration.
For example:
for(Object element : collection){
collection.remove(element);
}
This snippet will always throw a ConcurrentModificationException (except for empty collections) even though only a single thread is at work. We are mutating the collection while an iterator is still active. Maybe specialized collections, such as the CopyOnWriteArrayList, can survive this loop without throwing an exception, but the general Collection contract specifies that this is an invalid case.
A nice summary!
One small comment: a
ConcurrentModificationException
does not only occur when two threads access the same collection. It may also happen that your own (single-threaded) code iterates over a collection, and makes changes to it during the iteration.For example:
This snippet will always throw a
ConcurrentModificationException
(except for empty collections) even though only a single thread is at work. We are mutating the collection while an iterator is still active. Maybe specialized collections, such as theCopyOnWriteArrayList
, can survive this loop without throwing an exception, but the generalCollection
contract specifies that this is an invalid case.Thank you, I am update the post