前言

既然是绕过迭代器遍历时的数据批改异样,那么有必要先看一下是什么样的异样。如果在汇合的迭代器遍历时尝试更新汇合中的数据,比方像上面这样,我想输入 Hello,World,Java,迭代时却发现多了一个 C++ 元素,如果间接删除掉的话。

List<String> list = new ArrayList<>();Collections.addAll(list, "Hello", "World", "C++", "Java");// 我想输入 Hello,World,Java,迭代时发现多一个 C++,所以间接删除掉。Iterator iterator = list.iterator();System.out.println(iterator.next());System.out.println(iterator.next());list.remove("C++");System.out.println(iterator.next());

<!-- more -->

那么我想你肯定会遇到一个异样 ConcurrentModificationExceptio

HelloWorldjava.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)

这个异样在刚开始学习 Java 或者应用其余的非线程平安的汇合过程中可能都有遇到过。导致这个报错呈现的起因就和咱们操作的一样,对于某些汇合,不倡议在遍历时进行数据批改,因为这样会数据呈现不确定性。

那么如何绕过这个谬误呢?这篇文章中脑洞大开的三种形式肯定不会让你悲观。

异样起因

这不是一篇源码剖析的文章,然而为了介绍绕过这个异样呈现的起因,还是要提一下的,曾经晓得的同学能够间接跳过。

依据下面的报错,能够追踪到报错地位 ArrayList.java 的 857 行和 907 行,追踪源码能够发现在迭代器的 next 办法的第一行,调用了 checkForComodification() 办法。

而这个办法间接进行了一个把变量 modCountexpectedModCount 进行了比照,如果不统一就会抛出来 ConcurrentModificationException 异样。

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 数据类型的值的最大值溢出到正数,再持续减少直到变回原来的值呢?如果能够这样,首先要有一种操作能够在更新列表构造的同时不批改数据。为此翻阅了源码寻找这样的办法。还真的存在这样的办法。

public void trimToSize() {    modCount++;    if (size < elementData.length) {        elementData = (size == 0)          ? EMPTY_ELEMENTDATA          : Arrays.copyOf(elementData, size);    }}

上来就递增了 modCount,同时没有批改任何数据,只是把数据的存储进行了压缩。

List<String> list = new ArrayList<>();Collections.addAll(list, "Hello", "World", "C++", "Java");list.listIterator();Iterator iterator = list.iterator();System.out.println(iterator.next());System.out.println(iterator.next());list.remove("C++");// 40 多亿次遍历,溢出到正数,持续溢出到原值for (int n = Integer.MIN_VALUE; n < Integer.MAX_VALUE; n++) ((ArrayList) list).trimToSize();System.out.println(iterator.next());

正确输入了想要的 Hello,World,Java

绕过形式二:线程加对象锁绕过

剖析一下咱们的代码,每次输入的都是 System.out.println(iterator.next());。能够看进去是先运行了迭代器 next 办法,而后才运行了System.out 进行输入。所以第二种思路是先把第三个元素C++ 更新为Java ,而后启动一个线程,在迭代器再次调用 next 办法后,把第四个元素移除掉。这样就输入了咱们想要的后果。

List<String> list = new ArrayList<>();Collections.addAll(list, "Hello", "World", "C++", "Java");list.listIterator();Iterator iterator = list.iterator();System.out.println(iterator.next());System.out.println(iterator.next());// 开始操作list.set(2, "Java");Phaser phaser = new Phaser(2);Thread main = Thread.currentThread();new Thread(() -> {    synchronized (System.out) {        phaser.arriveAndDeregister();        while (main.getState() != State.BLOCKED) {            try {                Thread.sleep(100);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        list.remove(3);    }}).start();phaser.arriveAndAwaitAdvance();System.out.println(iterator.next());// 输入汇合System.out.println(list);/** * 失去输入 *  * Hello * World * Java * [Hello, World, Java] */

正确输入了想要的 Hello,World,Java 。这里简略说一下代码中的思路,Phaser 是 JDK 7 的新增类,是一个阶段执行处理器。结构时的参数 parties 的值为2,阐明须要两个参与方实现时才会进行到下一个阶段。而 arriveAndAwaitAdvance 办法被调用时,能够让一个参与方达到。

所以线程中对 System.out 进行加锁,而后执行 arriveAndAwaitAdvance 使一个参与方报告实现,此时会阻塞,等到另一个参与方报告实现后,线程进入到一个主线程不为阻塞状态时的循环。

这时主线程执行 System.out.println(iterator.next()); 。获取到迭代器的值进行输入时,因为线程内的加锁起因,主线程会被阻塞。晓得线程内把汇合的最初一个元素移除,线程解决实现才会持续。

绕过形式三:利用类型擦除放入魔法对象

在创立汇合的时候为了缩小谬误概率,咱们会应用泛型限度放入的数据类型,其实呢,泛型限度的汇合在运行时也是没有限度的,咱们能够放入任何对象。所以咱们能够利用这一点做些文章。

List<String> list = new ArrayList<>();Collections.addAll(list, "Hello", "World", "C++", "Java");list.listIterator();Iterator iterator = list.iterator();System.out.println(iterator.next());System.out.println(iterator.next());// 开始操作((List)list).set(2, new Object() {    public String toString() {        String s = list.get(3);        list.remove(this);        return s;    }});System.out.println(iterator.next());

代码里间接把第三个元素放入了一个魔法对象,重写了 toString() 办法,内容是返回汇合的第四个元素,而后删除第三个元素,这样就能够失去想要的 Hello,World,Java 输入。

下面就是绕过迭代器遍历时的数据批改报错的三种办法了,不论实用性如何,我感觉每一种都是大开脑洞的操作,这些操作都须要对某个知识点有肯定的理解,关注我,理解更多稀奇古怪的开发技巧

参考

[1] https://www.javaspecialists.e...

订阅

文章曾经收录到 github.com/niumoo/javanotes

也能够关注我的博客或者微信搜寻「 未读代码 」。

文章会在博客和公众号同步更新。