Fail Safe and Fail Fast


Fail Fast: When we try to attempt concurrent modification or structural modification, It will not allow that to happen rather it will leave to exception. 


Eg: In case of for loop when we are adding the data while we are accessing then it will result in never ending loop. 

for (int i=0; i<al.size();i++)

{

System.out.println(al.get(i));

al.add(99)//

}

To overcome this we will use Iterator. If we use Iterator instead of for loop It will result in Fail Fast by giving exception.

Iterator itr=al.iterator();

while(itr.hasNext()){

system.out.println(itr.hasNext());

}


Fail Safe: It is a mechanism which will stop concurrent modification or structural modification without giving any exception.

U need to use CopyOnWriteArrayList class to achieve this


So, we will import the concurrent packages when we want to achieve Fail Safe 




Comments

Popular posts from this blog

Advantages and Disadvantages of Monolithic Architecture

Monolithic Architecture

Eclipse debugging