<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 37  文章 - 29  trackbacks - 0


    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    最新評論

    閱讀排行榜

    評論排行榜

    工作中碰到個ConcurrentModificationException。代碼如下:
    List list = ...;
    for(Iterator iter = list.iterator(); iter.hasNext();) {
        Object obj = iter.next();
        ...
        if(***) {
            list.remove(obj);
        }
    }
    在執行了remove方法之后,再去執行循環,iter.next()的時候,報java.util.ConcurrentModificationException(當然,如果remove的是最后一條,就不會再去執行next()操作了)

    下面來看一下源碼
    public interface Iterator<E> {
        boolean hasNext();
        E next();
        void remove();
    }

    public interface Collection<E> extends Iterable<E> {
        ...
        Iterator<E> iterator();
        boolean add(E o);
        boolean remove(Object o);
        ...
    }

    這里有兩個remove方法

    接下來來看看AbstractList
    public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { 
    //AbstractCollection和List都繼承了Collection
        protected transient int modCount = 0;
        private class Itr implements Iterator<E> {  //內部類Itr
            int cursor = 0;
            int lastRet = -1;
            int expectedModCount = modCount;

            public boolean hasNext() {
                return cursor != size();
            }

            public E next() {
                checkForComodification();  //特別注意這個方法
                try {
                    E next = get(cursor);
                    lastRet = cursor++;
                    return next;
                } catch(IndexOutOfBoundsException e) {
                    checkForComodification();
                    throw new NoSuchElementException();
                }
            }

            public void remove() {
                if (lastRet == -1)
                    throw new IllegalStateException();
                checkForComodification();

                try {
                    AbstractList.this.remove(lastRet);  //執行remove對象的操作
                    if (lastRet < cursor)
                        cursor--;
                    lastRet = -1;
                    expectedModCount = modCount;  //重新設置了expectedModCount的值,避免了ConcurrentModificationException的產生
                } catch(IndexOutOfBoundsException e) {
                    throw new ConcurrentModificationException();
                }
            }

            final void checkForComodification() {
                if (modCount != expectedModCount)  //當expectedModCount和modCount不相等時,就拋出ConcurrentModificationException
                    throw new ConcurrentModificationException();
            }
        }   
    }


    remove(Object o)在ArrayList中實現如下:
    public boolean remove(Object o) {
        if (o == null) {
                for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                return true;
            }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }
    private void fastRemove(int index) {
        modCount++;  //只增加了modCount
        ....
    }

    所以,產生ConcurrentModificationException的原因就是:
    執行remove(Object o)方法之后,modCount和expectedModCount不相等了。然后當代碼執行到next()方法時,判斷了checkForComodification(),發現兩個數值不等,就拋出了該Exception。
    要避免這個Exception,就應該使用remove()方法。

    這里我們就不看add(Object o)方法了,也是同樣的原因,但沒有對應的add()方法。一般嘛,就另建一個List了


    下面是網上的其他解釋,更能從本質上解釋原因:
    Iterator 是工作在一個獨立的線程中,并且擁有一個 mutex 鎖。 Iterator 被創建之后會建立一個指向原來對象的單鏈索引表,當原來的對象數量發生變化時,這個索引表的內容不會同步改變,所以當索引指針往后移動的時候就找不到要迭代的對象,所以按照 fail-fast 原則 Iterator 會馬上拋出 java.util.ConcurrentModificationException 異常。
    所以 Iterator 在工作的時候是不允許被迭代的對象被改變的。但你可以使用 Iterator 本身的方法 remove() 來刪除對象, Iterator.remove() 方法會在刪除當前迭代對象的同時維護索引的一致性。

    http://gceclub.sun.com.cn/yuanchuang/week-14/iterator.html

    posted on 2008-08-31 19:44 EvanLiu 閱讀(92477) 評論(3)  編輯  收藏 所屬分類: Java基礎

    FeedBack:
    # re: java.util.ConcurrentModificationException 2011-11-30 03:28 子墨
    用remove(index)方法就不會有這種情況  回復  更多評論
      
    # re: java.util.ConcurrentModificationException[未登錄] 2012-03-06 13:58 飛天奔月
    @子墨 +1
      回復  更多評論
      
    # re: java.util.ConcurrentModificationException 2012-10-12 00:28 gKiwi
    今天也恰好遇到這個問題用的是Map.Entry。
    可是Entry沒有remove方法?
    再瞅瞅~  回復  更多評論
      
    主站蜘蛛池模板: 久久久久国产成人精品亚洲午夜 | 亚洲AV成人噜噜无码网站| 国产成人A在线观看视频免费| 亚洲av日韩aⅴ无码色老头| 亚洲中文字幕无码日韩| 精品无码国产污污污免费网站| 亚洲午夜福利在线视频| 成人亚洲性情网站WWW在线观看| 最近的中文字幕大全免费8| 亚洲爆乳成av人在线视菜奈实| 亚洲人成77777在线播放网站| 久久受www免费人成_看片中文| 人妻免费久久久久久久了| 亚洲免费电影网站| 亚洲中文字幕无码爆乳AV| 免费看国产成年无码AV片| 中文字幕不卡高清免费| 国产亚洲福利在线视频| 亚洲av无码一区二区三区网站| 免费看片A级毛片免费看| 一级毛片免费观看不卡视频| 美女一级毛片免费观看| 亚洲小说图片视频| 亚洲人成无码网站| 国产精品久免费的黄网站| 最刺激黄a大片免费网站| 一区视频免费观看| 国产成人精品亚洲日本在线| 亚洲婷婷五月综合狠狠爱| 日本免费一区二区三区最新vr| 最近免费中文字幕mv在线电影| 一区二区3区免费视频| 亚洲午夜福利在线视频| 亚洲午夜电影在线观看高清| 久久久久亚洲精品天堂久久久久久 | 成人福利在线观看免费视频| xxx毛茸茸的亚洲| 狠狠久久永久免费观看| 日韩电影免费在线观看网站| 亚洲大码熟女在线观看| WWW亚洲色大成网络.COM|