Fail-Fast vs. Fail-Safe - Edited
Fail-Fast vs. Fail-Safe - Edited
Faizan Rasool
Computer Programming
19 Dec 2021
Fail-Fast:
Practically:
import java.util.*;
integ.add(10);
integ.add(11);
integ.add(16);
while(itrt.hasNext()){
Integer a=itrt.next();
Integer.remove(a);
Faizy 2
Fail-Safe:
Fail-Safe iterator means they will never throw an exception even if the collection is
modified while iterating over it. When you get the iterator from the underlying collection and
While iterating the collection if you do some kind of basically Structural Modification means if
you are adding or removing elements then this will not throw any kind of concurrent
Modification Exception. So such iterator may work on the clone of the collection instead of the
original
Example:
import java.util.*;
integ.add(10);
integ.add(11);
integ.add(16);
Iterator<Integer> itrt = integ.iterator();
Faizy 3
while(itrt.hasNext()){
Integer a=itrt.next();
System.out.println(a);
integ.add(a);