Sobre excepción de modificación concurrente [cerrado]
¿Podría decirnos si hay alguna forma en la que la excepción de modificación concurrente pueda ocurrir en un entorno de un solo subproceso? La aplicación que aparece a continuación consta de dos subprocesos. asesorar
package concurrentmodificationError;
import java.util.*;
class ItrDemo
{
public static void main(String arg[])
{
Vector v=new Vector();
v.addElement("Amit");
v.add("Rahul");
v.add(1,"Nitin");
v.addElement("Ankit");
System.out.println("There are "+v.size()+"elements in the vector ");
final Iterator itr=v.iterator();
Thread th=new Thread() {
public void run()
{
System.out.println("New Thread started,traversing elements of vector...");
System.out.println("Contents of vector are...");
while(itr.hasNext())
{
System.out.println(itr.next());
try
{
Thread.sleep(2000);
}
catch(Exception e1)
{
}
}
}
};// end of annomyous class
System.out.println("Suspending main thread and starting a new thread for traversing the contents of vector...");
th.start();
try
{
Thread.sleep(1000);
}
catch(Exception e1)
{
}
System.out.println("main thread resumed,modifying vector...");
v.remove("Ankit");
v.add("Puneet");
v.add("Raman");
System.out.println("Vector Modified , Ankit removed and Puneet & Raman added.");
}
}
Sí, tengo entendido que en el entorno de un solo subproceso este error puede venir ... como se muestra en la siguiente pieza de código ...
System.out.println("Content of list are : ");
ListIterator itr1=list.listIterator();
while(itr1.hasNext())
{
list.add(new Emp("Anand","Manager",56000)); //
Emp e=(Emp)itr1.next();
e.display();
}
Por favor avise cuales son las formas de superarlo ... ¡para que no reciba este error ..!