Java Collections supports two types of Iterator, fail safe and fail fast. The main distinction between a fail fast and fail safe Iterator is whether or not the underlying collection can be modified while its begin iterated. If you have used Collection like ArrayList then you know that when you iterate over them, no other thread should modify the collection. If Iterator detects any structural change after iteration has begun e.g adding or removing a new element then it throws
ConcurrentModificationException, this is known as fail-fast behavior and these iterator are called
fail-fast iterator because they fail as soon as they detect any modification . Though its not necessary that iterator will throw this exception when multiple threads modified it simultaneously. it can happen even with single thread when you try to remove elements by using ArrayList's remove() method instead of Iterator's remove method, as discussed in my earlier post,
2 ways to remove objects from ArrayList.
Most of the Collection classes from Java 1.4 e.g.
Vector,
ArrayList,
HashMap, HashSet has fail-fast iterators. The other type of iterator was introduced in Java 1.5 when concurrent collection classes e.g.
ConcurrentHashMap,
CopyOnWriteArrayList and
CopyOnWriteArraySet was introduced. These iterator uses a view of original collection for doing iteration and that's why they doesn't throw
ConcurrentModificationException even when original collection was modified after iteration has begun. This means you could iterate and work with stale value, but this is the cost you need to pay for fail-safe iterator and this feature is clearly documented