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

          ConcurrentModificationException

          Posted on 2012-07-17 17:01 timelyxyz 閱讀(110) 評論(0)  編輯  收藏

          突然拋了一個concurrentModificationException錯誤,Iterator的一個基本概念沒有掌握導致的這個錯誤,就是在Iterator的實現類。比如Hashtable里面的內部類
           private class Enumerator<T> implements Enumeration<T>, Iterator<T>

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

           1 public T next() {
           2      if (modCount != expectedModCount)
           3          throw new ConcurrentModificationException();
           4      return nextElement();
           5  }
           6 
           7 
           8 public void remove() {
           9      if (!iterator)
          10         throw new UnsupportedOperationException();
          11      if (lastReturned == null)
          12         throw new IllegalStateException("Hashtable Enumerator");
          13      if (modCount != expectedModCount)
          14         throw new ConcurrentModificationException();
          15 
          16      synchronized(Hashtable.this) {
          17         Entry[] tab = Hashtable.this.table;
          18         int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;
          19 
          20      for (Entry<K,V> e = tab[index], prev = null; e != null; prev = e, e = e.next) {
          22       if (e == lastReturned) {
          23          modCount++;
          24          expectedModCount++;
          25          if (prev == null)
          26             tab[index] = e.next;
          27          else
          28             prev.next = e.next;
          29          count--;
          30          lastReturned = null;
          31          return;
          32       }
          33      }
          34      throw new ConcurrentModificationException();
          35      }
          36     }
          37     }
          而自己在next的同時,修改了這個集合,導致了這個錯誤的出現

           

          在Map或者Collection的時候,不要用它們的API直接修改集合的內容,如果要修改可以用Iterator的remove()方法,例如:
          1 public void setReparation( Reparation reparation ) {
          2         for (Iterator it = this.reparations.iterator();it.hasNext();){    //reparations為Collection
          3             Reparation repa = (Reparation)it.next();
          4             if (repa.getId() == reparation.getId()){
          5                 this.reparations.remove(repa);
          6                 this.reparations.add(reparation);
          7             }
          8         }
          9    }

           

          如上寫會在運行期報ConcurrentModificationException,可以如下修改:

           

           1  public void setReparation( Reparation reparation ) {
           2         boolean flag = false;
           3         for (Iterator it = this.reparations.iterator();it.hasNext();){    //reparations為Collection
           4             Reparation repa = (Reparation)it.next();
           5             if (repa.getId() == reparation.getId()){
           6                 it.remove();
           7                 flag = true;
           8                 break;
           9             }
          10         }
          11         if(flag){
          12           this.reparations.add(reparation);
          13         }
          14     }

           

           

          原文摘自 alreal 

           


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 云南省| 昌都县| 澄迈县| 云浮市| 横山县| 那曲县| 万年县| 鄂托克旗| 手机| 高安市| 牟定县| 普洱| 鄂伦春自治旗| 淳安县| 哈尔滨市| 宜州市| 南部县| 江津市| 淳化县| 江油市| 扶绥县| 云浮市| 聊城市| 桦川县| 肇州县| 定西市| 九龙坡区| 鄂州市| 曲沃县| 天柱县| 怀远县| 尼木县| 芜湖县| 织金县| 临湘市| 称多县| 博罗县| 景泰县| 马边| 金华市| 灵宝市|