Prateek Prateek - 2 months ago 20
Java Question

What are fail-safe & fail-fast Iterators in Java

There are two types of iterators in Java: fail-safe and fail-fast.

What does this mean, and is the difference between them?

Answer Source

What is the difference between them ...

"Fail safe" means: it won't fail. Strictly speaking, there is no such thing in Java as a fail-safe iterator. The correct term is "weakly consistent". The javadoc says:

"Most concurrent Collection implementations (including most Queues) also differ from the usual java.util conventions in that their Iterators and Spliterators provide weakly consistent rather than fast-fail traversal."

Typically, weak consistency means that if a collection is modified concurrently with an iteration, the guarantees of what the iteration sees are weaker. (The details will be specified in in the collection classes javadocs.)

"Fail fast" means: it may fail ... and the failure condition is checked aggressively so that the failure condition is detected before damage can be done. In Java, a fail-fast iterator fails by throwing a ConcurrentModificationException.

The alternative to "fail-fast" and "weakly consistent" is a semantic where the iteration fails unpredictably; e.g. to sometimes give the wrong answer or throw a totally unexpected exception. (This was the behavior of some standard implementations of the Enumeration API in early versions of Java.)

... and are they different from the iterator we use for collection.

No. These are properties of the iterators implemented by standard Collection types; i.e. they are either "fail fast" or "weakly consistent" ... when used correctly with respect to synchronization and the Java memory model1.


The fail-fast iterators are typically implemented using a volatile counter on the collection object.

  • When the collection is updated, the counter is incremented.
  • When an Iterator is created, the current value of the counter is embedded in the Iterator object.
  • When an Iterator operation is performed, the method compares the two counter values and throws a CME if they are different.

The implementation of fail-safe iterators is typically light-weight. They typically rely on properties of the specific list implementation's data structures. There is no general pattern. (Read the source code for the specific collection classes you are interested in.)


1 - Note that I said "correctly with respect to synchronization and the memory model". That means that (for example) if you iterate an ArrayList without proper synchronization, the end result could be a corrupted list result. The "fast fail" mechanism will probably detect the concurrent modification (though that isn't guaranteed), but it won't detect the underlying corruption. As an example, javadoc for Vector.iterator() says this:

"The fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs."