关于并发修改异常 [英] Regarding concurrent modification Exception

查看:83
本文介绍了关于并发修改异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能否告诉我在单线程环境中是否有任何方式可以发生并发修改异常,我发布的下面的应用程序包含两个线程,请告诉我在单个线程中也可以看到相同的异常..请告知

Could you please tell is there any way in which concurrent modification exception could occur in a single threaded environment also the below application which I am posting consists of two threads , please tell me can I see the same exception in a single thread also..please advise

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.");
  }

}

是的,我得到了那些人单线程enviorement这个错误可以来..如下面的代码片段..

yeah I got it folks that in the single threaded enviorement this error can comes.. as shown in below piece of code..

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();
          }

请告知有什么方法可以克服它...所以没有得到这个错误.. !!请指教

Please advise what are the ways to overcome it..so that does not get this error..!!Please advise

推荐答案

A ConcurrentModificationException 可以是抛出单线程环境。只要在不应该​​在其他线程中的上下文中修改对象,就会使用它。

A ConcurrentModificationException can be thrown in a single thread environment. It is used whenever an object is modified in a context where it shouldn't, not necessarily in another thread.

示例:

public class CME {
    public static void main(String...args) {
        HashSet<Integer> coll = new HashSet<Integer>();
        coll.add(1);
        coll.add(2);
        coll.add(3);

        for(Integer i : coll) {
            coll.remove(i); // Throws ConcurrentModificationException
        }
    }
}

这篇关于关于并发修改异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆