Difference between fail-safe vs fail-fast Iterator in Java? Example (original) (raw)
The difference between fail-safe and fail-fast Iterator is becoming favorite core java interview questions day by day, the reason it touches concurrency a bit, and the interviewee can go deep on it to ask how fail-safe or fail-fast behavior is implemented. In this article, we will see what are fail-safe and fail-fast iterators in java and the differences between fail-fast and fail-safe iterators. The concept of the fail-safe iterator is relatively new in Java and was first introduced with Concurrent Collections in Java 5 like ConcurrentHashMap and CopyOnWriteArrayList.
Difference between fail-fast Iterator vs fail-safe Iterator in Java
Without wasting any more of your time, here is some important points to know about both fail-safe and fail-fast iterators in Java.
1. fail-fast Iterators in Java
As the name suggests fail-fast Iterators fail as soon as they realized that structure of the Collection has been changed since iteration has begun. Structural changes mean adding, removing or updating any element from the collection while one thread is Iterating over that collection.
The fail-fast behavior is implemented by keeping a modification count and if the iteration thread realizes the change in modification count it throws ConcurrentModificationException.
Java doc says this is not a guaranteed behavior instead its done of "best effort basis", So application programming can not rely on this behavior.
Also since multiple threads are involved while updating and checking modification count and this check is done without synchronization, there is a chance that the Iteration thread still sees a stale value and might not be able to detect any change done by parallel threads.
Iterators returned by most of the JDK1.4 collection are fail-fast including Vector, ArrayList, HashSet, etc. to read more about Iterator see my post What is Iterator in Java.
2. fail-safe Iterator in java
Contrary to fail-fast Iterator, the fail-safe iterator doesn't throw any Exception if Collection is modified structurally while one thread is Iterating over it because they work on a clone of Collection instead of the original collection and that’s why they are called a fail-safe iterator.
Iterator of CopyOnWriteArrayList is an example of a fail-safe Iterator also iterator written by ConcurrentHashMap keySet is also a fail-safe iterator and never throw ConcurrentModificationException in Java.
If you like to see the difference in tabular format, here is a nice table highlighting difference between fail-safe and fail-fast Iterator in Java:
That’s all on the difference between fail-safe vs fail-fast Iterator in Java, As I said due to there confusing or not to easy differentiation they are quickly becoming popular java collection questions asked on various level of java interview. Let me know if you are aware of any other difference between fail-fast and fail-safe iterator.
Other Core Java Interview questions you may like
Thanks for reading this article so far. If you have been ask this core Java question on interviews, then please let us know, if you have any thing to add or any doubt feel free to ask I will try my best to answer your queries.