Sunday, 3 June 2018


What is fail-fast, fail-safe and ConcurrentModificationException in java?

 Iterator returned by few Collection framework Classes are fail-fast,  means any structural  modification made to these classes during iteration will throw ConcurrentModificationException in java.

Iterator returned by few Collection framework Classes are fail-safe, means any structural modification made to these classes during iteration won’t throw any Exception in java.

package javaresearch;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListExample {
           
              public static void main(String args[]) {
                         
          // creates array with initial capacity of 10.
          List<String>  arrayList= new ArrayList<String>();

        arrayList.add("Govind");
        arrayList.add("Ballabh");

          // fail-fast
     Iterator<String> iterator = arrayList.iterator();
          while (iterator.hasNext()) {
                 arrayList.add("Khan"); // unComment to avoid ConcurrentModificationException
                 System.out.println(iterator.next());
          }

          // fail-fast
          ListIterator<String> listIterator = arrayList.listIterator();
          listIterator.next();
          listIterator.previous();
          while (listIterator.hasNext()) {
                 arrayList.add("Java"); // unComment to avoid ConcurrentModificationException
                 System.out.println(listIterator.next());
          }

          // fail-fast
          Enumeration<String> listEnum = Collections.enumeration(arrayList);
          while (listEnum.hasMoreElements()) {
                 arrayList.add("Technology"); // unComment to avoid ConcurrentModificationException
                 System.out.println(listEnum.nextElement());
          }

          //enhanced for loop is fail-fast
          for (String string : arrayList) {
                 arrayList.add("Madhubani-Bihar"); // unComment to avoid ConcurrentModificationException
                 System.out.println(string);
          }



   }

}



package javaresearch;

import java.util.List;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListExample {
           
            public static void main(String args[]) {
                         
        // creates array with initial capacity of 10.
        List<String> copyOnWriteArrayList = new CopyOnWriteArrayList<String>();

        copyOnWriteArrayList.add("Govind");
        copyOnWriteArrayList.add("Ballabh");
 


        // fail-safe
        Iterator<String> iterator = copyOnWriteArrayList.iterator();
        while (iterator.hasNext()) {
              copyOnWriteArrayList.add("Khan");
               System.out.println(iterator.next());
        }
       
        System.out.println("-------------------------------------------------");

        for (String string : copyOnWriteArrayList) {
               System.out.println(string);
        }
       
        System.out.println("------------------------------------------");

        // fail-safe
        ListIterator<String> listIterator = copyOnWriteArrayList.listIterator();
       // listIterator.next();
        //listIterator.previous();
        while (listIterator.hasNext()) {
                    copyOnWriteArrayList.add("Java");
               System.out.println(listIterator.next());
        }
        System.out.println("------------------------------------------");
        // fail-safe
        Enumeration<String> listEnum = Collections
                     .enumeration(copyOnWriteArrayList);
        while (listEnum.hasMoreElements()) {
                   copyOnWriteArrayList.add("Technology");
               System.out.println(listEnum.nextElement());
        }
        System.out.println("------------------------------------------");
        //enhanced for loop is fail-safe
        for (String string : copyOnWriteArrayList) {
               copyOnWriteArrayList.add("Madhubani-Bihar");
               System.out.println(string);
        }


 }

}

o/p view:-----------------------


Govind
Ballabh
-------------------------------------------------
Govind
Ballabh
Khan
Khan
------------------------------------------
Govind
Ballabh
Khan
Khan
------------------------------------------
Govind
Ballabh
Khan
Khan
Java
Java
Java
Java
------------------------------------------
Govind
Ballabh
Khan
Khan
Java
Java
Java
Java
Technology
Technology
Technology
Technology
Technology
Technology
Technology
Technology


No comments:

Post a Comment