源碼揭祕LinkedList removeAll失敗原因

本來開開心心寫着代碼,然後一運行,一堆的錯誤信息,瞬間心情就不好了,生產代碼我這邊就不貼出來了,下面老師以demo爲例,給大家分享一下這個難過的歷程。

public static void main(String[] args) {
    List<Integer> list = new LinkedList<>();
    list.add(1);
    list.add(2);
    int perCount = 100, index = 0;
    int times = list.size() / perCount;
    do {
        List<Integer> listTemp;
        if (list.size() >= perCount) {
            listTemp = list.subList(0, perCount);
        } else {
            listTemp = list.subList(0, list.size());
        }
        System.out.println(JSONArray.fromObject(listTemp));
        list.removeAll(listTemp);
        index++;
    }
    while (index <= times);
}

Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.SubList.checkForComodification(AbstractList.java:769)
	at java.util.SubList.listIterator(AbstractList.java:695)
	at java.util.AbstractList.listIterator(AbstractList.java:299)
	at java.util.SubList.iterator(AbstractList.java:691)
	at java.util.AbstractCollection.contains(AbstractCollection.java:99)
	at java.util.AbstractCollection.removeAll(AbstractCollection.java:375)

上面的代碼看着沒啥問題,那List提供的removeAll方法爲什麼就報錯了呢?不要着急,我們根據報錯信息一步步分析。

首先我們要清楚LinkedList類的繼承關係,如果是用IDEA開發的童靴,可以直接Ctrl+H查看類的繼承關係。

AbstractCollection (java.util)
    AbstractList (java.util)
        AbstractSequentialList (java.util)
            LinkedList (java.util)
                KeepAliveStreamCleaner (sun.net.www.http)

因爲是在執行list.removeAll(listTemp)的時候報錯的,list.removeAll(listTemp)方法在AbstractCollection類中,我們進入看看這個方法是如何實現的。

AbstractCollection

public boolean removeAll(Collection<?> c) {
    Objects.requireNonNull(c);
    boolean modified = false;
    Iterator<?> it = iterator();
    while (it.hasNext()) {
        if (c.contains(it.next())) {
            it.remove();
            modified = true;
        }
    }
    return modified;
}

我們可以看到這個方法的主要就是獲取list集合的迭代器,如果list集合中的元素包含在listTemp集合中,就remove掉。我們看到錯誤棧中錯誤的源頭是c.contains(c爲AbstractCollection類對象),我們進去看看。

public boolean contains(Object o) {
    Iterator<E> it = iterator();
    if (o==null) {
        while (it.hasNext())
            if (it.next()==null)
                return true;
    } else {
        while (it.hasNext())
            if (o.equals(it.next()))
                return true;
    }
    return false;
}

這個方法就更簡單了,獲取listTemp的迭代器,然後循環判斷是否存在參數中的o元素,再對比錯誤棧,發現出錯的地方是再 Iterator<E> it = iterator()中,iterator()方法來源於AbstractList類,我們進去這個類中看看。

AbstractList

 public ListIterator<E> listIterator(final int index) {
        checkForComodification();
        rangeCheckForAdd(index);

        return new ListIterator<E>() {
            private final ListIterator<E> i = l.listIterator(index+offset);

            public boolean hasNext() {
                return nextIndex() < size;
            }

            public E next() {
                if (hasNext())
                    return i.next();
                else
                    throw new NoSuchElementException();
            }

            public boolean hasPrevious() {
                return previousIndex() >= 0;
            }

            public E previous() {
                if (hasPrevious())
                    return i.previous();
                else
                    throw new NoSuchElementException();
            }

            public int nextIndex() {
                return i.nextIndex() - offset;
            }

            public int previousIndex() {
                return i.previousIndex() - offset;
            }

            public void remove() {
                i.remove();
                SubList.this.modCount = l.modCount;
                size--;
            }

            public void set(E e) {
                i.set(e);
            }

            public void add(E e) {
                i.add(e);
                SubList.this.modCount = l.modCount;
                size++;
            }
        };
    }
private void checkForComodification() {
    if (this.modCount != l.modCount)
        throw new ConcurrentModificationException();
}

代碼很多,但是關鍵在於this.modCount != l.modCount這兩個不相等,拋出異常導致的錯誤。我們首先要知道this.modCount、l.modCount分別對應着哪個類對象。this代表本身,所以就是開頭的listTemp集合對象了,那l呢,跟蹤l的賦值情況,我們可以得到l就是list集合對象,也就是我們需要刪除元素的集合。那問題就可以轉化爲:爲什麼list的modCount和listTemp的modCount不一致呢,是哪個地方修改了它?

細心的童靴們不知道發現沒,在上面有一個it.remove()方法(it就是list集合),我們進去這個方法中看看。

LinkedList

public void remove() {
    checkForComodification();
    if (lastReturned == null)
        throw new IllegalStateException();
    Node<E> lastNext = lastReturned.next;
    unlink(lastReturned);
    if (next == lastReturned)
        next = lastNext;
    else
        nextIndex--;
    lastReturned = null;
    expectedModCount++;
}
E unlink(Node<E> x) {
    // assert x != null;
    final E element = x.item;
    final Node<E> next = x.next;
    final Node<E> prev = x.prev;
    
    if (prev == null) {
        first = next;
    } else {
        prev.next = next;
        x.prev = null;
    }
    
    if (next == null) {
        last = prev;
    } else {
        next.prev = prev;
        x.next = null;
    }

    x.item = null;
    size--;
    modCount++;
    return element;
}

罪魁禍首終於找到了,就是因爲remove()方法的unlink方法導致的,最終導致兩個對象的modCount大小不一致而拋出異常。

總結

問題雖然得到解決,但是modCount的作用是什麼?爲什麼這種情況下兩者的值會不一樣呢?LinkedList的removeAll()會出現這種問題,但是ArrayList的removeAll()爲什麼不會呢?有興趣的同學可以自己研究一下。

要更多幹貨、技術猛料的孩子,快點拿起手機掃碼關注我,我在這裏等你哦~

林老師帶你學編程https://wolzq.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章