java 多线程遍历list_在多线程的情况下是由Iterator遍历修改集合对象,报ConcurrentModificationException()异常的根因分析。...

在多线程下使用Iterator来迭代对象时,总会包ConcurrentModificationException();异常,经过我对list和Iterator相关源码的分析,终于搞明白了这个问题:

下面结合源代码来讨论这个问题:

1、当我们调用一个List的iterator时,其实返回的并不是Itr对象(Iterator是一个接口),请看代码:

1

1fa987a29c6482f53d401256f96355eb.png

ca75c07623e1b494fee67e8f316fc310.gifpublicIteratoriterator()9b8a8a44dd1c74ae49c20a7cd451974e.png{2d18c02628675d0a2c816449d98bda930.pngreturnnewItr();38f1ba5b45633e9678d1db480c16cae3f.png  }

2、在Itr类中有这么一句话  int expectedModCount = modCount;首先讲一下这两个变量的含义:

expectedModCount :创建当前的Itr对象时集合对象被修改的次数。他是Itr的变量。

modCount:记录集合对象从创建到当前时间被做修改的次数。(集合每进行一次增删改查都会使modCount),他是Itr的外部类AbstractList

的变量,且该变量在 AbstractList中被如此修饰protected transient int modCount = 0;

另外,Itr自己对集合对象进行了修改后,他会维持expectedModCount 和modCount的保持相等,请看以下代码

1 //Itr的删除方法2 publicvoidremove() {3 if(lastRet==-1)4 thrownewIllegalStateException();5checkForComodification();67 try{8 AbstractList.this.remove(lastRet);9 if(lastRet

在代码的12行,Itr保证了expectedModCount 和modCount值的相等 ,modCount的值发生了改变吗,他改变了,带代码的第8行改变的,请看此处的代码:

1

1fa987a29c6482f53d401256f96355eb.png

ca75c07623e1b494fee67e8f316fc310.gifpublicE remove(intindex)9b8a8a44dd1c74ae49c20a7cd451974e.png{2d18c02628675d0a2c816449d98bda930.png    RangeCheck(index);3d18c02628675d0a2c816449d98bda930.png4d18c02628675d0a2c816449d98bda930.png    modCount++;5d18c02628675d0a2c816449d98bda930.png    E oldValue=elementData[index];6d18c02628675d0a2c816449d98bda930.png7d18c02628675d0a2c816449d98bda930.pngintnumMoved=size-index-1;8d18c02628675d0a2c816449d98bda930.pngif(numMoved>0)9d18c02628675d0a2c816449d98bda930.png        System.arraycopy(elementData, index+1, elementData, index,10d18c02628675d0a2c816449d98bda930.png                 numMoved);11d18c02628675d0a2c816449d98bda930.png    elementData[--size]=null;//Let gc do its work12d18c02628675d0a2c816449d98bda930.png13d18c02628675d0a2c816449d98bda930.pngreturnoldValue;148f1ba5b45633e9678d1db480c16cae3f.png    }

在代码的第四行modCount发生了改变。 由此可以看出,在我们调用集合对象的iterator()方法的remove时总会使list的modCount的值自增1,但是Itr会自己维护该值和expectedModCount 的一致。

3、试问:如果expectedModCount 和modCount的值如果不相等,会有什么问题呢,这就是报ConcurrentModificationException();异常的原因所在,请先看Itr的next()方法和next()调用的方法

1

4f1150b881333f12a311ae9ef34da474.png//Itr的next方法:21fa987a29c6482f53d401256f96355eb.png

ca75c07623e1b494fee67e8f316fc310.gifpublicE next()9b8a8a44dd1c74ae49c20a7cd451974e.png{3d18c02628675d0a2c816449d98bda930.png            checkForComodification();497e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.giftry9b8a8a44dd1c74ae49c20a7cd451974e.png{5d18c02628675d0a2c816449d98bda930.png        E next=get(cursor);6d18c02628675d0a2c816449d98bda930.png        lastRet=cursor++;7d18c02628675d0a2c816449d98bda930.pngreturnnext;897e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gif        }catch(IndexOutOfBoundsException e)9b8a8a44dd1c74ae49c20a7cd451974e.png{9d18c02628675d0a2c816449d98bda930.png        checkForComodification();10d18c02628675d0a2c816449d98bda930.pngthrownewNoSuchElementException();11ecedf933ec37d714bd4c2545da43add2.png        }128f1ba5b45633e9678d1db480c16cae3f.png    }134f1150b881333f12a311ae9ef34da474.png144f1150b881333f12a311ae9ef34da474.png//checkForComodification()方法:151fa987a29c6482f53d401256f96355eb.png

ca75c07623e1b494fee67e8f316fc310.giffinalvoidcheckForComodification()9b8a8a44dd1c74ae49c20a7cd451974e.png{16d18c02628675d0a2c816449d98bda930.pngif(modCount!=expectedModCount)17d18c02628675d0a2c816449d98bda930.pngthrownewConcurrentModificationException();188f1ba5b45633e9678d1db480c16cae3f.png    }194f1150b881333f12a311ae9ef34da474.png    }204f1150b881333f12a311ae9ef34da474.png

在next方法的一开始显示调用了checkForComodification()方法(见第三行),在checkForComodification()方法中做的工作就是比较expectedModCount 和modCount的值是否相等,如果不相等,就认为还有其他对象正在对当前的List进行操作,那个就会抛出ConcurrentModificationException异常。

经过以上的分析,发现抛出ConcurrentModificationException异常处于调用next()方法时,比较expectedModCount 和modCount的值,如果两个值不相等,就会抛出异常,然而在什么情况下会使expectedModCount 和modCount的值不相等呢,只有在两个Itr同时对一个list进行操作的时候才会出现这样的问题,所以在以后的编码过程中在是由Iterator进行remove()时一定要考虑是否时多线程的,如果是请不要用Iterator进行remove(),而应该使用List的remove方法进行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值