Monday, July 16, 2018

what do you mean by Fail Fast & Fail Safe iterators in java?

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. 

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

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...