What is the difference between fail-fast and fail-safe iteartors in java?
fail-safe iterator is relatively new concept in comparison to the fail-fast iterator concept.
1)fail-fast iterator:fail-fast iterators fail as soon as they realize that structure of the collection has been modified since iteration has begin.Here structural changes means either some element has been added,deleted or updated.fail-fast iterators are implemented by keeping the modification count.So if there is some modification in collection after iteration started,it will throw a ConcurrentModificationException.Iterators returned by Vector, ArrayList, HashSet , HashMap etc are fail-fast.
2)fail-safe Iterators:fail-safe iterators don't throw any error when there is any modification during iteration.This is because iteration is done on the clone of collection instead of original collection.So thats why they are known as fail-safe because they will not throw ConcurrentModificationException whenever there is modification in collections. Iterators returned by CopyOnWriteArrayList is an example of fail-safe iterator.
fail-safe iterator is relatively new concept in comparison to the fail-fast iterator concept.
1)fail-fast iterator:fail-fast iterators fail as soon as they realize that structure of the collection has been modified since iteration has begin.Here structural changes means either some element has been added,deleted or updated.fail-fast iterators are implemented by keeping the modification count.So if there is some modification in collection after iteration started,it will throw a ConcurrentModificationException.Iterators returned by Vector, ArrayList, HashSet , HashMap etc are fail-fast.
2)fail-safe Iterators:fail-safe iterators don't throw any error when there is any modification during iteration.This is because iteration is done on the clone of collection instead of original collection.So thats why they are known as fail-safe because they will not throw ConcurrentModificationException whenever there is modification in collections. Iterators returned by CopyOnWriteArrayList is an example of fail-safe iterator.
0 comments:
Post a Comment