Drop Down

Tuesday, March 5, 2019

Concurrent Modification Exception

package concurrent_Collection;
/*One Thread iterating Collection Object and other thread trying to modify it...
 This leads to Concurrent Modification Exception
java.util.ConcurrentModificationException*/

import java.util.*;

public class ConcurrentModificationException extends Thread
{
static ArrayList list = new ArrayList();
public void run()
{
try
{
  Thread.sleep(700);
}
catch(Exception e)
{
System.out.println(e.toString());
}
System.out.println("Child Thread adding ---> 10");
list.add(10);
}

public static void main(String[] args) throws Exception
{
  list.add(59);
  list.add(788);
  list.add(45);
  list.add(88);
  ConcurrentModificationException thread = new ConcurrentModificationException();
  thread.start();
  Iterator itr = list.iterator();
  while(itr.hasNext())
  {
  System.out.println(itr.next());
  Thread.sleep(500);
  }

}

}

No comments:

Post a Comment

Java 8 Notes Pics