0

I am developing a System and it has ArrayList that access in several places(inserting, removing and updates the values). Due to access of ArrayList in several places when i run the program it gives Concurrent update error.

Instead of ArrayList I can use Vector because Vector is synchronized. But if i use Vector will It be cause to decrease the performance of the system? Give me Ideas. How I can solve this issue?

This is part of the exception I get:

].[localhost].[/uckt].[Faces Servlet]] (http-127.0.0.1-8080-144) 
Servlet.service() for servlet Faces Servlet threw exception: java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source) [:1.7.0_02]
at java.util.ArrayList$Itr.next(Unknown Source) [:1.7.0_02] 
assylias
  • 321,522
  • 82
  • 660
  • 783
Pradeep Gamage
  • 585
  • 4
  • 8
  • 21
  • 2
    Do you access your arraylist from several threads? You can also get a ConcurrentModificationException if you remove items from a list while iterating for example (unless you use iterator.remove()). – assylias Aug 13 '12 at 11:07
  • yes. I am getting this error. So i wanted to prevent it. – Pradeep Gamage Aug 13 '12 at 11:11
  • In that case you probably don't need a thread safe variant of the an ArrayList - see [this post for an example](http://stackoverflow.com/questions/5113016/concurrentmodificationexception-when-remove-element-from-a-java-util-list-while). – assylias Aug 13 '12 at 11:14
  • 2
    @PradeeGamage You should post the part of the code that throws the exception with a stacktrace to get better and more specific answers. – assylias Aug 13 '12 at 11:19
  • This is part of exception ].[localhost].[/uckt].[Faces Servlet]] (http-127.0.0.1-8080-144) Servlet.service() for servlet Faces Servlet threw exception: java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) [:1.7.0_02] at java.util.ArrayList$Itr.next(Unknown Source) [:1.7.0_02] – Pradeep Gamage Aug 13 '12 at 11:26

4 Answers4

2

There is one more thing: Your ConcurrentModificationException might not spawn from an actual concurrent modification through two Threads. There is another possible reason:

While iterating over the ArrayList, you might delete an element. If you try this

Object o = iterator.next()
if(someCondition)
    arrayList.remove(o)

in a single Thread, you will get a ConcurrentModificationException. In that case you will have to use ListIterator and it's remove method.

John Smith
  • 2,282
  • 1
  • 14
  • 22
1

Have you considered the CopyOnWriteArrayList ?

A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

As ever, I would not worry too much about efficiency until you know it's a problem.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

See Collections#synchronizedCollection(Collection). This will return a synchronized ArrayList that you can use to safely add/remove/update elements in the list.

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
0

You can also synchronize your ArrayList on each access:

synchronize(myList)
{
  myList.add(object);
}
Peter Ilfrich
  • 3,727
  • 3
  • 31
  • 35