一次線上java.util.ConcurrentModificationException異常排查引發(fā)的思考

異常拋出原因

在使用remove方法對(duì)ArrayList進(jìn)行刪除操作時(shí),會(huì)拋出此異常。

代碼分析

測(cè)試用戶類:

package sort;

public class User implements Comparable<User>{

    private int id;

    private String name;

    private int birthDay;

    public User(int id, String name, int birthDay) {
        this.id = id;
        this.name = name;
        this.birthDay = birthDay;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getBirthDay() {
        return birthDay;
    }

    @Override
    public int compareTo(User user) {
        return user.getId() - this.getId();
    }
}

各種remove方法測(cè)試:

package sort;

import com.google.common.collect.Lists;

import java.util.Iterator;
import java.util.List;

public class SortTest1 {

    public static void main(String[] args) {
        foreachRemove();
        iteratorRemove();
        iteratorHasNextCheck();
    }

    private static void iteratorHasNextCheck() {
        List<User> list = initUserList();
        Iterator<User> iterator = list.iterator();
        while(iterator.hasNext()){
            User user = iterator.next();
            if("四".equals(user.getName())){
                list.remove(user);
            }else{
                System.out.println(user.getName());
            }
        }
    }

    private static void iteratorRemove() {
        try {
            List<User> list = initUserList();
            Iterator<User> iterator = list.iterator();
            while (iterator.hasNext()) {
                User user = iterator.next();
                if (user.getBirthDay() < 20040101) {
                    iterator.remove();
                }
            }
            System.out.println(list.size());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("iteratorRemove failed");
        }
    }

    private static void foreachRemove() {
        try {
            List<User> list = initUserList();
            for (User user : list) {
                if (user.getBirthDay() < 20040101) {
                    list.remove(user);
                }
            }
            System.out.println(list.size());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("foreachRemove failed");
        }

    }

    private static List<User> initUserList() {
        List<User> list = Lists.newArrayList();
        list.add(new User(1,"一", 20010101));
        list.add(new User(2,"二", 20020202));
        list.add(new User(3,"三", 20030303));
        list.add(new User(4,"四", 20040404));
        list.add(new User(5,"五", 20050505));
        return list;
    }
}

結(jié)果:

java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
    at java.util.ArrayList$Itr.next(ArrayList.java:859)
    at sort.SortTest1.foreachRemove(SortTest1.java:49)
    at sort.SortTest1.main(SortTest1.java:11)
foreachRemove failed
2
一
二
三

在分析結(jié)果前,先貼出ArrayList的迭代器源碼

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 boolean hasNext() {
            return cursor != size;
        }

        @SuppressWarnings("unchecked")
        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];
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void forEachRemaining(Consumer<? super E> consumer) {
            Objects.requireNonNull(consumer);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i >= size) {
                return;
            }
            final Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length) {
                throw new ConcurrentModificationException();
            }
            while (i != size && modCount == expectedModCount) {
                consumer.accept((E) elementData[i++]);
            }
            // update once at end of iteration to reduce heap write traffic
            cursor = i;
            lastRet = i - 1;
            checkForComodification();
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

分析:
1.可以看到hasNext()方法主要是比對(duì)當(dāng)前元素的數(shù)組下標(biāo)與迭代器元素的個(gè)數(shù)。
2.next方法要先檢查ArrayList的操作數(shù)(modCount,ArrayList父類AbstractList的成員變量,
這個(gè)成員變量記錄著集合的修改次數(shù),也就每次add或者remove它的值都會(huì)加1)有沒有改變,如果改變就會(huì)拋出ConcurrentModificationException異常。

  1. foreachRemove里使用了ArrayList的remove方法,modCount發(fā)生了變化,所以拋出異常。
  2. iteratorRemove里使用了ArrayList內(nèi)部類Itr的remove方法,modCount未發(fā)生變化,所以正常執(zhí)行。
  3. iteratorHasNextCheck里正好移除了第4個(gè)元素,此時(shí)雖然modCount變化了,但是元素總數(shù)與索引下標(biāo)都是4,此時(shí)hasNext()返回false,所以不會(huì)往下執(zhí)行。
  4. iteratorHasNextCheck里如果移除前3個(gè)元素,依舊會(huì)拋出ConcurrentModificationException異常。

線上問(wèn)題

線上使用guava本地緩存,并且每次拿出數(shù)據(jù)會(huì)再次進(jìn)行Collections.sort()排序,導(dǎo)致拋出此異常。
偽代碼
:

package sort;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SortTest2 {

    private static Map<String, List<User>> map = Maps.newHashMap();

    public static void main(String[] args) {
        buildMap();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5,
                0L, TimeUnit.MILLISECONDS,  new LinkedBlockingQueue<Runnable>());
        for (int i=0; i<5; i++) {
            executor.execute(() -> {
                try {
                    Collections.sort(map.get("list"));
                    Thread.sleep(1000L);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("sort thread error");
                }
            });
        }
        executor.shutdown();
    }

    private static void buildMap() {
        map.put("list", initUserList());
    }

    private static List<User> initUserList() {
        List<User> list = Lists.newArrayList();
        list.add(new User(3,"三", 20030303));
        list.add(new User(2,"二", 20020202));
        list.add(new User(4,"四", 20040404));
        list.add(new User(1,"一", 20010101));
        list.add(new User(5,"五", 20050505));
        return list;
    }
}

結(jié)果(報(bào)錯(cuò)數(shù)量不定,取決于并發(fā)數(shù)與排序執(zhí)行時(shí)間):

java.util.ConcurrentModificationException
    at java.util.ArrayList.sort(ArrayList.java:1464)
    at java.util.Collections.sort(Collections.java:141)
    at sort.SortTest2.lambda$main$0(SortTest2.java:24)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
sort thread error
java.util.ConcurrentModificationException
    at java.util.ArrayList.sort(ArrayList.java:1464)
    at java.util.Collections.sort(Collections.java:141)
    at sort.SortTest2.lambda$main$0(SortTest2.java:24)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
sort thread error
java.util.ConcurrentModificationException
    at java.util.ArrayList.sort(ArrayList.java:1464)
    at java.util.Collections.sort(Collections.java:141)
    at sort.SortTest2.lambda$main$0(SortTest2.java:24)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
sort thread error

ArrayList中的sort()方法

@Override
    @SuppressWarnings("unchecked")
    public void sort(Comparator<? super E> c) {
        final int expectedModCount = modCount;
        Arrays.sort((E[]) elementData, 0, size, c);
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }

主要調(diào)用了Arrays的sort()方法,此排序方法本文暫不介紹,后續(xù)modCount進(jìn)行了自增,此處在多個(gè)線程一起執(zhí)行下會(huì)出現(xiàn)問(wèn)題,拋出ConcurrentModificationException異常。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容