并发修改异常源码分析

//并发修改异常:运行以下下代码会抛出该异常--ConcurrentModificationException
  List<String> list = new ArrayList<String>();
        list.add("hello");
        list.add("world");
        list.add("java");

        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            String s = it.next();
            if (s.equals("hello")){
                list.add("javaee");
            }
            System.out.println(s);
        }

/*运行结果:
    hello
    Exception in thread "main" java.util.ConcurrentModificationException
      	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:911)
      	at java.util.ArrayList$Itr.next(ArrayList.java:861)
      	at it.heima.date0803.Demo.main(Demo.java:95)
异常指向next方法,该方法是迭代器it调用,迭代器是接口,由List对象调用iterator方法返回,List的对象是ArrayList
向上转型,找到ArrayList, 查看重写的iterator方法,并且我们使用list对象调用add方法,一并找到ArayList中的add
方法,继续跟进iterator()中返回的Itr,发现他是内部类,无参构造权限修饰符为默认,Itr() {},属性分别为
0,-1,0,然后调用next方法,方法中先调用checkForComodification();这个方法中做判断
if(modCount != expectedModCount),就会抛出并发修改异常ConcurrentModificationException();
在list.add时候,ArrayList对象调用了add方法,首先走这个ensureCapacityInternal(size + 1),
这里面 modCount++;所以Itr判断时候modCount != expectedModCount,抛出异常*/

     public class ArrayList<E> extends AbstractList<E>implements List<E>{
         public Iterator<E> iterator() {
                 return new Itr();
             }
         public boolean add(E e) {
             ensureCapacityInternal(size + 1);  // Increments modCount!!
             elementData[size++] = e;
             return true;
         }
     }

    // Itr类:
      private class Itr implements Iterator<E> {
             int cursor;       // index of next element to return
             int lastRet = -1; // index of last element returned; -1 if no such
             int expectedModCount = modCount;
             Itr() {}
             public E next() {
                 checkForComodification();
                 int i = cursor;
                 if (i >= size)
                     throw new NoSuchElementException();
                 Object[] elementData = ArrayList.this.elementData;
                 if (i >= elementData.length)
                     throw new ConcurrentModificationException();
                 cursor = i + 1;
                 return (E) elementData[lastRet = i];
             }
             final void checkForComodification() {
                 if (modCount != expectedModCount)
                     throw new ConcurrentModificationException();
             }
         }

 
       private void ensureExplicitCapacity(int minCapacity) {
              modCount++;
      
              // overflow-conscious code
              if (minCapacity - elementData.length > 0)
                  grow(minCapacity);
          }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兰林汉的驴粉丝儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值