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

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

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

    隨筆-59  評(píng)論-31  文章-0  trackbacks-0
    工作中碰到個(gè)ConcurrentModificationException。代碼如下:
    List list = ...;
    for(Iterator iter = list.iterator(); iter.hasNext();) {
        Object obj = iter.next();
        ...
        if(***) {
            list.remove(obj);
        }
    }
    在執(zhí)行了remove方法之后,再去執(zhí)行循環(huán),iter.next()的時(shí)候,報(bào)java.util.ConcurrentModificationException(當(dāng)然,如果remove的是最后一條,就不會(huì)再去執(zhí)行next()操作了)

    下面來(lái)看一下源碼
    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);
        ...
    }

    這里有兩個(gè)remove方法

    接下來(lái)來(lái)看看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> {  //內(nèi)部類(lèi)Itr
            int cursor = 0;
            int lastRet = -1;
            int expectedModCount = modCount;

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

            public E next() {
                checkForComodification();  //特別注意這個(gè)方法
                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);  //執(zhí)行remove對(duì)象的操作
                    if (lastRet < cursor)
                        cursor--;
                    lastRet = -1;
                    expectedModCount = modCount;  //重新設(shè)置了expectedModCount的值,避免了ConcurrentModificationException的產(chǎn)生
                } catch(IndexOutOfBoundsException e) {
                    throw new ConcurrentModificationException();
                }
            }

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


    remove(Object o)在ArrayList中實(shí)現(xiàn)如下:
    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
        ....
    }

    所以,產(chǎn)生ConcurrentModificationException的原因就是:
    執(zhí)行remove(Object o)方法之后,modCount和expectedModCount不相等了。然后當(dāng)代碼執(zhí)行到next()方法時(shí),判斷了checkForComodification(),發(fā)現(xiàn)兩個(gè)數(shù)值不等,就拋出了該Exception。
    要避免這個(gè)Exception,就應(yīng)該使用remove()方法。

    這里我們就不看add(Object o)方法了,也是同樣的原因,但沒(méi)有對(duì)應(yīng)的add()方法。一般嘛,就另建一個(gè)List了


    下面是網(wǎng)上的其他解釋?zhuān)軓谋举|(zhì)上解釋原因:
    Iterator 是工作在一個(gè)獨(dú)立的線(xiàn)程中,并且擁有一個(gè) mutex 鎖。 Iterator 被創(chuàng)建之后會(huì)建立一個(gè)指向原來(lái)對(duì)象的單鏈索引表,當(dāng)原來(lái)的對(duì)象數(shù)量發(fā)生變化時(shí),這個(gè)索引表的內(nèi)容不會(huì)同步改變,所以當(dāng)索引指針往后移動(dòng)的時(shí)候就找不到要迭代的對(duì)象,所以按照 fail-fast 原則 Iterator 會(huì)馬上拋出 java.util.ConcurrentModificationException 異常。
    所以 Iterator 在工作的時(shí)候是不允許被迭代的對(duì)象被改變的。但你可以使用 Iterator 本身的方法 remove() 來(lái)刪除對(duì)象, Iterator.remove() 方法會(huì)在刪除當(dāng)前迭代對(duì)象的同時(shí)維護(hù)索引的一致性。
    posted on 2012-01-06 17:14 RoyPayne 閱讀(207) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java并發(fā)
    主站蜘蛛池模板: CAOPORM国产精品视频免费| 亚洲精品天堂成人片?V在线播放| xxxxx做受大片在线观看免费| 亚洲精品美女久久久久| 亚洲精品无码久久久久去q | 亚洲精品在线视频| 免费观看黄网站在线播放| 大妹子影视剧在线观看全集免费| 亚洲av日韩专区在线观看| 国产成人精品亚洲日本在线| 亚洲网址在线观看你懂的| 亚洲色婷婷六月亚洲婷婷6月| 国产精品四虎在线观看免费| 亚洲免费网站观看视频| 久久免费的精品国产V∧| 国产中文字幕在线免费观看| 日韩a毛片免费观看| 在线精品自拍亚洲第一区| 亚洲精品中文字幕无码A片老| 亚洲av无码一区二区三区观看| 亚洲男人的天堂在线播放| 亚洲第一AAAAA片| 亚洲国产精品VA在线看黑人| 精品国产亚洲男女在线线电影| 免费国产在线观看老王影院| 国产精品免费播放| 国产精品无码一区二区三区免费 | 久久久亚洲精品无码| 亚洲情综合五月天| 亚洲欧洲成人精品香蕉网| 亚洲码国产精品高潮在线| 在线亚洲人成电影网站色www| 亚洲最大激情中文字幕| 亚洲精品少妇30p| 亚洲av永久无码精品国产精品| 亚洲αv久久久噜噜噜噜噜| 久久亚洲国产视频| 久久亚洲国产成人精品性色| 亚洲色图黄色小说| 亚洲 日韩 色 图网站| 亚洲AV无码一区二区三区性色 |