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

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

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

    關于 java.util.ConcurrentModificationException 異常

            網上的解釋原因:
    Iterator 是工作在一個獨立的線程中,并且擁有一個 互斥 鎖。 Iterator 被創建之后會建立一個指向原來對象的單鏈索引表,當原來的對象數量發生變化時,這個索引表的內容不會同步改變,所以當索引指針往后移動的時候就找不到要迭代的對象,所以按照 fail-fast 原則 Iterator 會馬上拋出 java.util.ConcurrentModificationException 異常。
    所以 Iterator 在工作的時候是不允許被迭代的對象被改變的。但你可以使用 Iterator 本身的方法 remove() 來刪除對象, Iterator.remove() 方法會在刪除當前迭代對象的同時維護索引的一致性。  
               
              廖雪峰 大師  Java源碼分析:深入探討Iterator模式  http://gceclub.sun.com.cn/yuanchuang/week-14/iterator.html


    在工作碰到這個問題所有看看:
    List list = ...;
    for(Iterator iter = list.iterator(); iter.hasNext();) {
        Object obj = iter.next();
        ...
        if(***) {
            list.remove(obj);  //list.add(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);
        ...
    }



    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()方法了,只能另建一個list來處理這個問題了。



          

    posted on 2009-08-04 14:51 胡鵬 閱讀(322) 評論(0)  編輯  收藏 所屬分類: java基礎

    導航

    <2009年8月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    統計

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    agile

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 中文字幕免费视频一| 日韩在线视频线视频免费网站| 日本XXX黄区免费看| 国产精品69白浆在线观看免费| 在线天堂免费观看.WWW| 亚洲AV日韩AV永久无码下载| 51精品视频免费国产专区| 中文字幕人成无码免费视频| 久久精品国产亚洲AV麻豆不卡| 亚洲av永久无码精品网站| sss在线观看免费高清| 美女啪啪网站又黄又免费| 怡红院亚洲红怡院在线观看| 成人黄动漫画免费网站视频 | 免费看又爽又黄禁片视频1000| 久久中文字幕免费视频| 国产自产拍精品视频免费看| 欧洲黑大粗无码免费| 99精品全国免费观看视频| 亚洲国产综合人成综合网站00| 国产精品亚洲а∨天堂2021| 亚洲一级片免费看| 永久免费视频v片www| 亚洲伊人久久精品| 一级做a爱片特黄在线观看免费看| 一区二区免费视频| 情人伊人久久综合亚洲| 亚洲乱妇熟女爽到高潮的片| 美女啪啪网站又黄又免费| 四虎AV永久在线精品免费观看| 国产V亚洲V天堂无码| 暖暖免费在线中文日本| 无码人妻丰满熟妇区免费| 久久久久亚洲精品成人网小说| 99国产精品免费观看视频| 日本zzzzwww大片免费| 国产va免费精品观看精品| 亚洲一线产品二线产品| 免费乱理伦在线播放| 久久久受www免费人成| 中字幕视频在线永久在线观看免费|