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

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

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

    關(guān)于 java.util.ConcurrentModificationException 異常

            網(wǎng)上的解釋原因:
    Iterator 是工作在一個(gè)獨(dú)立的線程中,并且擁有一個(gè) 互斥 鎖。 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ù)索引的一致性。  
               
              廖雪峰 大師  Java源碼分析:深入探討Iterator模式  http://gceclub.sun.com.cn/yuanchuang/week-14/iterator.html


    在工作碰到這個(gè)問(wèn)題所有看看:
    List list = ...;
    for(Iterator iter = list.iterator(); iter.hasNext();) {
        Object obj = iter.next();
        ...
        if(***) {
            list.remove(obj);  //list.add(obj)  其實(shí)也是一樣的。
        }
    }

    在執(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);
        ...
    }



    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();  //該方法中檢測(cè)到改變,從而拋出異常
                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()方法,但是沒(méi)有add()方法了,只能另建一個(gè)list來(lái)處理這個(gè)問(wèn)題了。



          

    posted on 2009-08-04 14:51 胡鵬 閱讀(322) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java基礎(chǔ)

    導(dǎo)航

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

    統(tǒng)計(jì)

    常用鏈接

    留言簿(3)

    隨筆分類(lèi)

    隨筆檔案

    agile

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 精品国产香蕉伊思人在线在线亚洲一区二区| 亚洲国产精品无码av| 免费手机在线看片| 亚洲日韩精品A∨片无码| 亚洲精品国产自在久久 | 一级毛片免费全部播放| 亚洲国产另类久久久精品| 一二三四影视在线看片免费 | 免费91麻豆精品国产自产在线观看 | 青青操在线免费观看| 亚洲男人的天堂久久精品| 亚洲免费日韩无码系列 | 国产精品亚洲аv无码播放| 免费人成在线观看69式小视频| 国产成人高清亚洲一区91| 亚洲天天做日日做天天看| 国产在线观看免费不卡 | 免费精品人在线二线三线区别| 日本永久免费a∨在线视频| 亚洲精品高清国产麻豆专区| 亚洲国产成人乱码精品女人久久久不卡 | 亚洲人6666成人观看| 亚洲午夜久久久影院| 精品国产免费一区二区| 久久久久国色av免费看| 国产亚洲视频在线| 91亚洲自偷在线观看国产馆| 久久久久无码专区亚洲av| 蜜臀91精品国产免费观看| 国产大片91精品免费观看不卡| 视频免费1区二区三区| 日本亚洲色大成网站www久久| 亚洲阿v天堂在线| 亚洲国产精品尤物yw在线 | 国产精品免费观看久久| 久久久99精品免费观看| 国产成人自产拍免费视频| 色五月五月丁香亚洲综合网| 亚洲成人一级电影| 久久久亚洲欧洲日产国码农村| 亚洲精品成人片在线观看|