Problem statement: what do you mean by Fail Fast & Fail Safe Iterator in java?
Iterators in java are used to iterate over the Collection objects.
Fail-Fast iterators immediately throw ConcurrentModificationException if there is structural modification of the collection. Structural modification means adding, removing or updating any element from collection while a thread is iterating over that collection.
Iterator on ArrayList, HashMap classes are examples of fail-fast Iterator.
Fail-Safe iterators don’t throw any exceptions if a collection is structurally modified while iterating over it. This is because, they operate on the clone of the collection, not on the original collection and that’s why they are called fail-safe iterators.
Fail-Safe iterators don’t throw any exceptions if a collection is structurally modified while iterating over it. This is because, they operate on the clone of the collection, not on the original collection and that’s why they are called fail-safe iterators.
Iterator on ConcurrentHashMap, CopyOnWriteArrayList classes are examples of fail-safe Iterator.
2nd Explanations:
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators
Fail-Safe iterators don't throw any exceptions if the collection is modified while iterating over it. Because, they iterate on the clone of the collection not on the actual collection
Fail-Safe iterators don't throw any exceptions if the collection is modified while iterating over it. Because, they iterate on the clone of the collection not on the actual collection
No comments:
Post a Comment