About Concurrent Modification Exception
来源:互联网 发布:中银淘宝信用卡额度 编辑:程序博客网 时间:2024/06/08 12:44
About Concurrent Modification Exception
This exception is not necessarily thrown in a multithreaded code. It happens when you modify a collection while it is being iterated. You can get this exception even in single-threaded applications. For instance, in a for-each loop, if you remove or add elements to a list, you end up getting a ConcurrentModificationException
.
As such, adding synchronization to the code will not necessarily solve the problem. Some alternatives consist in making a copy of the data to be iterated, or using iterators that accept modifications (i.e. ListIterator), or a collection with snapshot iterators.
Evidently, in a multithreaded piece of code, you would still have to take care of synchronization to avoid further problems.
Let me give some examples:
Let's say you want to delete items from a collection while iterating over it. Your alternatives to avoid aConcurrentModificationException
are:
List<Book> books=newArrayList<Book>();books.add(newBook(new ISBN("0-201-63361-2")));books.add(newBook(new ISBN("0-201-63361-3")));books.add(newBook(new ISBN("0-201-63361-4")));
Collect all the records that you want to delete within an enhanced for loop, and after you finish iterating, you remove all found records.
ISBN isbn = new ISBN("0-201-63361-2");List<Book> found = newArrayList<Book>();for(Book book: books){if(book.getIsbn().equals(isbn)){ found.add(book);}}books.removeAll(found);
Or you may use a ListIterator
which has support for a remove/add method during the iteration itself.
ListIterator<Book> iter= books.listIterator();while(iter.hasNext()){if(iter.next().getIsbn().equals(isbn)){ iter.remove();}}
In a multithreaded environment, you might consider making a copy of the collection before iterating, as such, allowing others to modify the original collection without affecting iteration:
synchronized(this.books){List<Book> copyOfBooks=newArrayList<Book>(this.books)}for(Book book : copyOfBooks){System.out.println(book);}
Alternatively, you may consider using other types of collections using snapshot iterators, likejava.util.ConcurrentCopyOnWriteArrayList
which guarantees not to throwConcurrentModificationException
. But read the documentation first, because this type of collection is not suitable for all scenarios.
From:http://stackoverflow.com/questions/11320310/how-can-java-util-concurrentmodificationexception-be-avoided-when-using-osc
- About Concurrent Modification Exception
- 解决Concurrent Modification Exception
- 20110110 About the ejTTS modification
- About Exception
- About exception
- About Exception
- Java Tips -- About Exception
- About exception of Java
- about Exception and soapSeralization
- About Exception Handling
- About Java Exception
- [c#] About exception
- Java Concurrent Modication Exception 解决
- [ORACLE EBS]6. About Concurrent Manager
- [ios] compile exception about ARC
- Java concurrent exception in the synchronized collection
- Modification For Linux or vxworks's TLB Data Miss Exception Handle On E500 Core(Powerpc Family)
- Useful Information about 'Workflow Background Process' Concurrent Program
- APP金准位,将推广进行到底
- 如何在Android Market中SEO你的APP应用
- 拨打电话简单Demo
- SQL 2008 服务启动失败的原因和解决方法汇总。
- 二叉树
- About Concurrent Modification Exception
- 1075 求年月日
- div+css布局(一)
- 让你低调的app脱颖而出
- 坑爹的冒泡排序,写傻逼了!
- Linux下LED驱动测试(应用程序)源码
- Fedora硬盘安装Fedora17图文教程
- PHP采集 抓取
- 最长公共子序列 南工36
Thank you for your answer. I am in fact doing exactly what you recommend in your second code example where elements are being removed after iteration. It is possible that something is being added to the list while it is being iterated. However, as far as I can tell it would only be happening as a result of a concurrent procedure. – jeremynealbrownJul 3 '12 at 23:06
@jeremynealbrown Then synchronization must be broken in your program somewhere. At some point, your collection is being modified while somewhere else is being iterated. Take into account that most collection iterators are backed by the original collection and that for-each loops use iterators behind the scenes. – Edwin DalorzoJul 3 '12 at 23:12
1
Wrapping the iteration processes in synchronized blocks seems to have fixed it quite well. Not only did it fix the problem, I also learned some really useful tricks that I'm sure I'll use again. Thanks for all of you help.!! – jeremynealbrownJul 3 '12 at 23:38