Hello
World
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:907)
at java.util.ArrayList$Itr.next(ArrayList.java:857)
at com.wdbyte.lab.jdk.ModCountDemo.updateCollections(ModCountDemo.java:26)
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
那么 modCount 这个变量存储的是什么信息呢?
/**
* The number of times this list has been <i>structurally modified</i>.
* Structural modifications are those that change the size of the
* list, or otherwise perturb it in such a fashion that iterations in
* progress may yield incorrect results.
*
* <p>This field is used by the iterator and list iterator implementation
* returned by the {@code iterator} and {@code listIterator} methods.
* If the value of this field changes unexpectedly, the iterator (or list
* iterator) will throw a {@code ConcurrentModificationException} in
* response to the {@code next}, {@code remove}, {@code previous},
* {@code set} or {@code add} operations. This provides
* <i>fail-fast</i> behavior, rather than non-deterministic behavior in
* the face of concurrent modification during iteration.
*
* <p><b>Use of this field by subclasses is optional.</b> If a subclass
* wishes to provide fail-fast iterators (and list iterators), then it
* merely has to increment this field in its {@code add(int, E)} and
* {@code remove(int)} methods (and any other methods that it overrides
* that result in structural modifications to the list). A single call to
* {@code add(int, E)} or {@code remove(int)} must add no more than
* one to this field, or the iterators (and list iterators) will throw
* bogus {@code ConcurrentModificationExceptions}. If an implementation
* does not wish to provide fail-fast iterators, this field may be
* ignored.
*/
protected transient int modCount = 0;
直接看源码注释吧,直接翻译一下意思就是说 modCount 数值记录的是列表的结构被修改的次数,结构修改是指那些改变列表大小的修改,或者以某种方式扰乱列表,从而使得正在进行的迭代可能产生不正确的结果。同时也指出了这个字段通常会在迭代器 iterator 和 listIterator 返回的结果中使用,如果 modCount 和预期的值不一样,会抛出 ConcurrentModificationException 异常。
而上面与 modCount 进行对比的字段 expectedModCount 的值,其实是在创建迭代器时,从 modCount 获取的值。如果列表结构没有被修改过,那么两者的值应该是一致的。 绕过方式一:40 多亿次循环绕过
上面分析了异常产生的位置和原因,是因为 modCount 的当前值和创建迭代器时的值有所变化。所以第一种思路很简单,我们只要能让两者的值一致就可以了。在源码 int modCount = 0; 中可以看到 modCount 的数据类型是 INT ,既然是 INT ,就是有数据范围,每次更新列表结构 modCount 都会增1,那么是不是可以增加到 INT 数据类型的值的最大值溢出到负数,再继续增加直到变回原来的值呢?如果可以这样,首先要有一种操作可以在更新列表结构的同时不修改数据。为此翻阅了源码寻找这样的方法。还真的存在这样的方法。