• Exception in thread "Thread-1" java.util.ConcurrentModificationException 异常原因和解决方法


    基本上所有的集合类都会有一个叫做快速失败的校验机制,当一个集合在被多个线程修改并访问时,就会出现ConcurrentModificationException 校验机制。它的实现原理就是我们经常提到的modCount修改计数器。如果在读列表时,modCount发生变化则会抛出ConcurrentModificationException异常。这与线程同步是两码事,线程同步是为了保护集合中的数据不被脏读、脏写而设置的。


    1、单线程环境下的异常重现

        public static void main(String[] args)  {
            ArrayList<Integer> list = new ArrayList<Integer>();
            list.add(2);
            Iterator<Integer> iterator = list.iterator();
            while(iterator.hasNext()){
                Integer integer = iterator.next();
                if(integer==2) {
                    list.remove(integer);
                    //iterator.remove();   //正确写法
                }
            }
        }
    

      在while(iterator.hasNext()) 循环遍历时,只允许删除ArrayList 内部的  elementData[ ] 的最后一个元素,而不允许从中间删除。
      在 iterator.next()  的源码中,会首先执行方法:checkForComodification();  该方法:

         final void checkForComodification() {
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
            }
    

      只有当两个变量值相等时才不会报错。而 list.add(2)操作和 list.remove(integer); 操作会使 modCount++;   但是变量 expectedModCount 是内部类的 Itr 子类 的 变量,该子类的实例化方法是:list.iterator();  实例对象时赋初始值:
    int expectedModCount = modCount;   所以,不允许在 while(iterator.hasNext())  循环方法内 有list.add(2)操作和 list.remove(integer);操作,否则会导致expectedModCount != modCount ,进而导致  throw new ConcurrentModificationException();


    2、多线程下的问题重现:
    public class Vector {
        public static void main(String[] args) {
            final List<String> tickets = new ArrayList<String>();
            for(int i=0;i<100000;i++){
                tickets.add("火车票"+i);
            }
            Thread returnThread = new Thread(){
                @Override
                public void run() {
                   while (true){
                       tickets.add("车票"+ new Random().nextInt());
                   }
                };
            };
            Thread saleThread = new Thread(){
                @Override
                public void run() {
                    for (String ticket : tickets){
                        tickets.remove(0);
                    }
                };
            };
            returnThread.start();
            saleThread.start();
        }
    }
    

      

      有可能有朋友说ArrayList是非线程安全的容器,换成Vector就没问题了,实际上换成Vector还是会出现这种错误。

      原因在于,虽然Vector的方法采用了synchronized进行了同步,但是实际上通过Iterator访问的情况下,每个线程里面返回的是不同的iterator,也即是说expectedModCount是每个线程私有。假若此时有2个线程,线程1在进行遍历,线程2在进行修改,那么很有可能导致线程2修改后导致Vector中的modCount自增了,线程2的expectedModCount也自增了,但是线程1的expectedModCount没有自增,此时线程1遍历时就会出现expectedModCount不等于modCount的情况了。

      因此一般有2种解决办法:

      1)在使用iterator迭代的时候使用synchronized或者Lock进行同步;

      2)使用并发容器CopyOnWriteArrayList代替ArrayList和Vector。

    
    
  • 相关阅读:
    第十二讲 Web 服务的创建和使用
    第十七讲 ASP.NET安全性
    第九讲 水晶报表的使用
    第十五讲 数据集的使用方法和技巧
    第十六讲 调试和跟踪ASP.NET应用程序
    第十讲 ASP.NET程序的部署
    第十四讲 ADO.NET数据操作
    第十八讲 Web服务器控件使用
    【笔记】java多线程 2 五种状态
    【笔记】数据库模式
  • 原文地址:https://www.cnblogs.com/yaohuiqin/p/9355874.html
Copyright © 2020-2023  润新知