Add to Google Reader or Homepage

java.util.ConcurrentModificationException

                           

                In this post we will see about the Concurrent Modification Exception which is usually thrown when a collection is structurally modified by more than one  iterators .Let us see in detail about this exception.

In java API it is given that,"If a method detects that an object is concurrently modified and when such a modification is not permissible, then this exception will be thrown".

 What does it mean?

 

This could be clearly explained in the context of collections, we all know that the collections are used to store the objects and there are iterators available,which contains methods that  enables us to traverse through the collections and  to add and remove elements at the desired locations.

This feature brings us a problem that "when two iterators are simultaneously used to modify the collection then this exception will be thrown" and there are two other situations at which this exception will be thrown, let's see what are they?

-->

Situation 1

 "Do not instantiate an iterator until the collection is filled with the objects", Let's see what happens if we instanitate an iterator before the collection is filled.


Program

import java.util.*;
import java.lang.*;
class Concurrenttest
{
public static void main(String args[])
{
LinkedList a=new LinkedList();
ListIterator li=a.listIterator();//line no 8
a.add("wow");
a.add("world");
a.add("ganesh");//line no 11
System.out.println(a);
System.out.println(li.next());// line no 13
}
}
 


Output

[wow, world, ganesh]
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
        at java.util.LinkedList$ListItr.next(Unknown Source)
        at Concurrenttest.main(Concurrenttest.java:13)

 Here in the above program you can see that i have just created a collection and stored in it  some string objects and i tried to traverse through the list but it throws Concurrent Modification Exception. But if you instantiate the iterator (line no 8)after the collection is filled(line no 11) then this exception would not be thrown.

Situation 2

 

 ''Do not  use the iterator and the collection object itself to modify the collection  simultaneously", Otherwise this exception would be thrown,

program

import java.util.*;
import java.lang.*;
class Concurrenttest
{
public static void main(String args[])
{
LinkedList a=new LinkedList();
a.add("wow");
ListIterator li=a.listIterator();
System.out.println(li.next());
a.add("world");//adding the object(modifying)
li.remove();// removing the object(modifying)
System.out.println(a);
}
}
 

Output

wow
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
        at java.util.LinkedList$ListItr.remove(Unknown Source)
        at Concurrenttest.main(Concurrenttest.java:12)
 
 In the above program you can see that i have used both the collection object and iterator to simultaneously modify the collection. In order to avoid this kind of problem, use any one,either iterator or collection object to modify the collection.

Use of Two Iterators simultaneously


This is the first reason which i have mentioned before,i.e, "using two iterators simultaneously to modify the collection".


Program

import java.util.*;
import java.lang.*;
class Concurrenttest
{
public static void main(String args[])
{
LinkedList a=new LinkedList();
a.add("wow");
a.add("world");
a.add("ganesh");
System.out.println(a);
ListIterator li=a.listIterator();
ListIterator li1=a.listIterator();
System.out.println(li.next());
li.remove();//one iterator removing the first object
li1.next();//Another iterator trying to read the first object that was removed
}
}

Output

[wow, world, ganesh]
wow
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
        at java.util.LinkedList$ListItr.next(Unknown Source)
        at Concurrenttest.main(Concurrenttest.java:16)

If you read the comment line of the above program you can understand what i have done.Thus in this case use only one iterator to modify and traverse the object  or use two iterators only to traverse the objects.


 

0 comments:

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top