日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ConcurrentModificationException 问题

發布時間:2025/5/22 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ConcurrentModificationException 问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:http://blog.csdn.net/phoenix2121/article/details/5757623

http://zhidao.baidu.com/question/271631895.html

方法一(效率不高):

這個問題是說,你不能在對一個List進行遍歷的時候將其中的元素刪除掉 解決辦法是,你可以先將要刪除的元素用另一個list裝起來,等遍歷結束再remove掉 可以這樣寫

List delList = new ArrayList();//用來裝需要刪除的元素 for(Information ia:list) if(ia.getId()==k){n++; delList.add(ia); } list.removeAll(delList);//遍歷完成后執行刪除

?

//

方法二:同步操作list時,一邊迭代remove,一邊新增.那么會報錯 java.util.ConcurrentModificationException

?

查看api發現vector有個好方法可以解決這個錯誤.

?

首先是用arraylist

// private Vector list;//兩種list方式private List list;public void init(){ // list = new Vector();list = new ArrayList();list.add("a");list.add("b");list.add("c");list.add("d");list.add("e");list.add("f");}public void removeIt(){//iteratorIterator it = list.iterator();for(int i=0;it.hasNext();i++){String a = (String)it.next();System.out.println(a);if(a.equals("c")){//list.remove(a);try {Thread.sleep(1000);} catch (InterruptedException e) {}it.remove();System.out.println("remove c");}}}// public void removeEm(){//enumeration // Enumeration e = list.elements(); // for(int i=0;e.hasMoreElements();i++){ // String a = (String)e.nextElement(); // System.out.println(a); // if(a.equals("c")){ // try { // Thread.sleep(1000); // } catch (InterruptedException ee) { // } // list.remove(a); // System.out.println("remove c"); // } // } // }public void add(){//先用迭代,再添加try {Thread.sleep(500);} catch (Exception e) {// TODO: handle exception }list.add("c");System.out.println("add c");}public void run() {removeIt(); // removeEm(); }public static void main(String[] args) {TestConcurrentModificationException t = new TestConcurrentModificationException();t.init();t.start();t.add();}

運行結果:

a
b
c
add c
Exception in thread "Thread-0" java.util.ConcurrentModificationException
?at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
?at java.util.AbstractList$Itr.remove(Unknown Source)
?at test.TestConcurrentModificationException.removeIt(TestConcurrentModificationException.java:33)
?at test.TestConcurrentModificationException.run(TestConcurrentModificationException.java:69)

?

?

?

其次是vector

?

private Vector list;//兩種list方式 // private List list;public void init(){list = new Vector(); // list = new ArrayList();list.add("a");list.add("b");list.add("c");list.add("d");list.add("e");list.add("f");}public void removeIt(){//iteratorIterator it = list.iterator();for(int i=0;it.hasNext();i++){String a = (String)it.next();System.out.println(a);if(a.equals("c")){//list.remove(a);try {Thread.sleep(1000);} catch (InterruptedException e) {}it.remove();System.out.println("remove c");}}}public void removeEm(){//enumerationEnumeration e = list.elements();for(int i=0;e.hasMoreElements();i++){String a = (String)e.nextElement();System.out.println(a);if(a.equals("c")){try {Thread.sleep(1000);} catch (InterruptedException ee) {}list.remove(a);System.out.println("remove c");}}}public void add(){//先用迭代,再添加try {Thread.sleep(500);} catch (Exception e) {// TODO: handle exception }list.add("c");System.out.println("add c");}public void run() { // removeIt(); removeEm();}public static void main(String[] args) {TestConcurrentModificationException t = new TestConcurrentModificationException();t.init();t.start();t.add();}

運行結果:

?

a
b
c
add c
remove c
e
f
c
remove c

api上說,

由 Vector 的 iterator 和 listIterator 方法所返回的迭代器是快速失敗的:如果在迭代器創建后的任意時間從結構上修改了向量(通過迭代 器自身的 remove 或 add 方法之外的任何其他方式),則迭代器將拋出 ConcurrentModificationException。因此,面對并發的修改,迭代器很快就完全失敗,而不是冒著在將來不確定的時間任意發生 不確定行為的風險。Vector 的 elements 方法返回的 Enumeration 不是 快速失敗的。

注意,迭代器的快速失敗行為不能得到保證,一般來說,存在不同步的并發修改時,不可能作出任何堅決的保證。快速失敗迭代器盡最大努力拋出 ConcurrentModificationException。因此,編寫依賴于此異常的程序的方式是錯誤的,正確做法是:迭代器的快速失敗行為應該僅用于檢測 bug。

?

?

當然如果使用其他的集合,那就沒有什么好辦法了.只有存放起來,每次取一個list對象,并不是實時.

?

?

?

?

轉載于:https://www.cnblogs.com/sunwufan/archive/2012/05/21/2511747.html

總結

以上是生活随笔為你收集整理的ConcurrentModificationException 问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。