Wednesday 18 November 2015

Explain about Fail-fast and Fail-safe with examples

fail-fast system is nothing but immediately report any failure . Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.

Ex: java.util package - HashMap, HashSet, TreeSet, etc.

Fail-Safe : It doesn't throw any Exception if Collection is modified structurally. while one thread is Iterating over it because they work on clone of Collection instead of original collection and that?s why they are called as fail-safe iterator.

Ex: java.util.concurrent - ConcurrentHashMap , ConcurrentSkipListSet, CopyOnWriteArrayList, ConcurrentMap.

No comments:

Post a Comment