About Concurrent Modification Exception

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.

 

 

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

 

 

 

From:http://stackoverflow.com/questions/11320310/how-can-java-util-concurrentmodificationexception-be-avoided-when-using-osc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值