posts - 9, comments - 8, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          ConcurrentModificationException

          Posted on 2007-01-23 11:31 Dr.Water 閱讀(3487) 評論(1)  編輯  收藏 所屬分類: Java 隨手貼

          ConcurrentModificationException
          一個不該犯的低級錯誤,今天的代碼突然拋了一個concurrentModificationException錯誤,
          Iterator的一個基本概念沒有掌握導致的這個錯誤,就是在Iterator的實現類
          比如Hashtable里面的內部類
          ?private class Enumerator<T> implements Enumeration<T>, Iterator<T>

          會在next,或者remove的時候檢查當前集合是否會在修改狀態,如果是的話
          就會拋出 ConcurrentModificationException,而他自己remove則是使用了同步的方法
          而且同步了modCount;expectedModCount;


          ?public T next() {
          ???? if (modCount != expectedModCount)
          ??throw new ConcurrentModificationException();
          ???? return nextElement();
          ?}


          public void remove() {
          ???? if (!iterator)
          ??throw new UnsupportedOperationException();
          ???? if (lastReturned == null)
          ??throw new IllegalStateException("Hashtable Enumerator");
          ???? if (modCount != expectedModCount)
          ??throw new ConcurrentModificationException();

          ???? synchronized(Hashtable.this) {
          ??Entry[] tab = Hashtable.this.table;
          ??int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;

          ??for (Entry<K,V> e = tab[index], prev = null; e != null;
          ?????? prev = e, e = e.next) {
          ????? if (e == lastReturned) {
          ???modCount++;
          ???expectedModCount++;
          ???if (prev == null)
          ?????? tab[index] = e.next;
          ???else
          ?????? prev.next = e.next;
          ???count--;
          ???lastReturned = null;
          ???return;
          ????? }
          ??}
          ??throw new ConcurrentModificationException();
          ???? }
          ?}
          ??? }
          而自己在next的同時,修改了這個集合,導致了這個錯誤的出現


          評論

          # re: ConcurrentModificationException  回復  更多評論   

          2010-11-01 13:00 by 呆呆地
          操 怎么修改你咋不說
          主站蜘蛛池模板: 南汇区| 南宫市| 叶城县| 招远市| 微山县| 河源市| 苏州市| 滨州市| 遂宁市| 香格里拉县| 合肥市| 泰顺县| 石首市| 北流市| 定陶县| 嘉禾县| 中宁县| 柏乡县| 垫江县| 开封县| 旅游| 连平县| 长丰县| 会泽县| 松滋市| 五家渠市| 湘潭县| 永安市| 谢通门县| 吉安市| 海南省| 北票市| 沂南县| 临夏县| 汉沽区| 枣强县| 徐汇区| 长汀县| 柘荣县| 开封市| 潜山县|