There are two types of iterators in Java: fail-safe and fail-fast.
What does this mean, and is the difference between them?
What is the difference between them ...
"Fail-safe" (in engineering) means that something fails in a way that causes no or minimal damage. Strictly speaking, there is no such thing in Java as a fail-safe iterator. If an iterator fails (in the normal sense of "fail"), you can expect damage to occur.
I suspect that you actually mean "weakly consistent" iterators. 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 each concurrent collection classes javadocs.)
"Fail-fast" (in systems design) means that the failure condition is checked aggressively so that the failure condition is (where possible1) detected before too much damage can be done. In Java, a fail-fast iterator fails by throwing a ConcurrentModificationException
.
The alternative to "fail-fast" and "weakly consistent" is semantic where the iteration fails unpredictably; e.g. to sometimes give the wrong answer or throw an 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.
Fail-fast iterators are typically implemented using a volatile
counter on the collection object.
Iterator
is created, the current value of the counter is embedded in the Iterator
object.Iterator
operation is performed, the method compares the two counter values and throws a CME if they are different.By contrast, weakly consistent iterators are typically light-weight and leverage properties of each concurrent collection's internal data structures. There is no general pattern. If you are interested, read the source code for different collection classes.
1 - The rider is that fail-fast behavior assumes that the application id correctly with respect to synchronization and the memory model. That means that (for example) if you iterate an ArrayList
without proper synchronization, the 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."
The only difference is fail-safe iterator doesn't throw any Exception, contrary to fail-fast Iterator.
If Collection is modified structurally while one thread is iterating over it. This is because they work on clone of Collection instead of original collection and that’s why they are called as fail-safe iterator.
Iterator of CopyOnWriteArrayList is an example of fail-safe Iterator also iterator written by ConcurrentHashMap keySet is also fail-safe iterator and never throw ConcurrentModificationException in Java.
This scenario relate with "concurrent processing", means that more then one user accessing the same resource. In such situation, one of the user try to modify that resource which cause the 'ConcurrentProcessingException' because in that case other user get improper data. Both this type relate with this kind of situation.
In simple term,
Fail-Fast :
Fail-Safe :
They are rather fail-fast and weakly-consistent types:
Iterators from java.util
package throw ConcurrentModificationException
if collection was modified by collection's methods (add / remove) while iterating
Iterators from java.util.concurrent
package typically iterate over a snapshot and allow concurrent modifications but may not reflect collection updates after the iterator was created.