0% found this document useful (0 votes)
65 views

Fail-Fast vs. Fail-Safe - Edited

Fail-Fast iterators throw ConcurrentModificationExceptions immediately if the underlying collection is modified during iteration. Fail-Safe iterators do not throw exceptions and instead work on a clone of the collection, merging modifications back to the original collection after iteration. For example, CopyOnWriteArrayList provides a Fail-Safe iterator that clones the collection before iteration and merges any modifications made during iteration back to the original collection.

Uploaded by

faizy gii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Fail-Fast vs. Fail-Safe - Edited

Fail-Fast iterators throw ConcurrentModificationExceptions immediately if the underlying collection is modified during iteration. Fail-Safe iterators do not throw exceptions and instead work on a clone of the collection, merging modifications back to the original collection after iteration. For example, CopyOnWriteArrayList provides a Fail-Safe iterator that clones the collection before iteration and merges any modifications made during iteration back to the original collection.

Uploaded by

faizy gii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Faizy 1

Faizan Rasool

Professor Mohib Ullah

Computer Programming

19 Dec 2021

What is the difference between Fail-Fast and Fail-Safe?

Fail-Fast:

In Fail-Fast, we can receive a concurrent modification exception if the collection is


modified while iterating over it, which is a runtime exception, and we don't need to catch this
kind of exception manually. The compiler will never say that you have to catch this exception
because it's a runtime exception. Since the iteration begins, the iterator fails as soon as it
realizes that a structure of the underlying data structure has been modified. So once you create
the iterator from the collection, let's say you have an Array List and you call the iterator method
on that then you get the return back as an iterator. So while you iterate the iterator when
another iterator will try to add or remove any elements from the underlying collection then
immediately you get a Concurrent Modification Exception on and that's why this is called the
Fail-Fast iteration method that is saving us from making corrupt underlying collection.

Practically:

import java.util.*;

public class failFastItrExample{

public static void main(String arguments[]){

List<Integer> integ= new ArrayList<>();

integ.add(10);

integ.add(11);

integ.add(16);

Iterator<Integer> itrt = integ.iterator();

while(itrt.hasNext()){

Integer a=itrt.next();
Integer.remove(a);

Faizy 2

System.out.println("Finally :: "+ integ);

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

collection such as a CopyOnRightArrayList. So this kind of implementation is how Java is


achieved. When you get an iterator from the underlying collection then that will make one
clone copy and all the modification happens on the clone copy and finally, that will merge to
the original collection, and that's the way Fail-Safe iterator works. It's the best example here
CopyOnWriteArrayList.

Example:

import java.util.*;

public class failSafeItrExample{

public static void main(String arguments[]){

List<Integer> integ= new CopyOnWriterArrayList<>();

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);

System.out.println("Finally :: "+ integ);

You might also like