Whenever we use java.util Collection classes, we have that if one thread changes a collection while another thread is traversing through it using an iterator, then any call to iterator.hasNext()
or iterator.next()
will throw ConcurrentModificationException
. Even the synchronized
collection wrapper classes SynchronizedMap
and SynchronizedList
are only conditionally thread-safe, which means all individual operations are thread-safe but compound operations where flow of control depends on the results of previous operations may be subject to threading issues. The question is: How to avoid this problem without affecting the performance. Note: I am aware of CopyOnWriteArrayList
.

- 46,613
- 43
- 151
- 237

- 721
- 5
- 12
-
2Then you already know your solution. You can't have your cake and eat it too: concurrent access entails a concurrent structure. – Marko Topolnik Nov 10 '12 at 14:20
-
1It is very case specific and the way you are using one. Also there needs to be consideration why you can not make the collection access and modification synchronized. – Amit Deshpande Nov 10 '12 at 14:22
-
3What is your **concrete** problem? You are talking about generic issues. – Adam Arold Nov 10 '12 at 14:25
-
I would return a copy of collection for `Iteration` same as what `CopyOnWriteArrayList` does – Amit Deshpande Nov 10 '12 at 14:33
-
I often (but not always) use CopyOnWriteArrayList to solve these type of problems. However sometimes it can be extremely costly. With this question I just would like to hear some opinions and alternatives addressing this issue. I will accept the answer that I see as a better (generic) alternative. – Bruno Simões Nov 10 '12 at 15:04
-
Related to http://stackoverflow.com/questions/1655362/concurrentmodificationexception-despite-using-synchronized – Raedwald Mar 28 '16 at 15:00
4 Answers
You can use CopyOnWriteArrayList
or ConcurrentHashMap
etc. as you mentioned above or you can use Atomic*
classes which are working with CAS.
If you weren't aware of Atomic*
classes they definitely worth a look! You may check out this question.
So to answer your question you have to choose the right tools for the task. Since you do not share the context with us I can just guess. In some situations CAS will perform better in others the concurrent Collections will.
If something isn't clear you can always check out the official Oracle Trails: Lesson: Concurrency

- 1
- 1

- 29,285
- 22
- 112
- 207
-
Not sure how a ConcurrentHashMap will help him, as the locks there are on segments of the map. – Yair Zaslavsky Nov 10 '12 at 14:22
-
1The `synchronized` decorator classes he mentioned are locking the whole `Map`. `ConcurrentHashMap` in the other hand only locks a bucket and only if you modify/delete. – Adam Arold Nov 10 '12 at 14:24
-
@Adam Arold - I understand, but I think it might be too risky. If I remember correctly, the synchronized decorator does not uses a RW-Lock, but uses "synchronized", this is why I suggested to lock but with RWLock at my comment. – Yair Zaslavsky Nov 10 '12 at 14:27
-
I see. Well it is hard to come up with a single case where those decorators are good. – Adam Arold Nov 10 '12 at 14:28
This is because the "standard" Java collections are not thread safe as they are not synchronized. When working with multiple threads accessing your collections, you should look at the java.util.concurrent packages.
Without this package, before Java 5, one had to perform a manual synchronization :
synchronized(list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
or using
Collections.synchronizedList(arrayList);
but neither could really offer a complete thread safety feature.
With this package, all access to the collection is made atomically and some classes provide a snapshot of the state of the list when the iterator was constructed
(see CopyOnWriteArrayList
. The CopyOnWriteArrayList
is fast on read, but if you are performing many writes, this might affect performance.
Thus, if CopyOnWriteArrayList
is not desired, take a look at ConcurrentLinkedQueue
which offers a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator
. This one is efficient in all point, unless you have to access elements at specific indexes more often than traversing the entire collection.
Another option would be ConcurrentSkipListSet
which provides expected average log(n) time cost for the contains, add, and remove operations and their variants. Insertion, removal, and access operations safely execute concurrently by multiple threads
and iterators are weakly consistent
as well.
Which concurrent (thread-safe) collections depend of what type of operations you perform the most with. And since they are all part of the Java Collection framework, you can swap them to which ever you need.

- 51,409
- 25
- 133
- 214
-
The comment is too general. You should have specified which classes could help him. java.util.concurrent is not a magic solution for concurrency if you don't know how to use it well – Yair Zaslavsky Nov 10 '12 at 14:20
-
OP is already aware not only of the package, but of the specific class that solves his problem. @AmitD This wouldn't fare well even as a comment :) For the record, Yanick, I'm not one of your downvoters. – Marko Topolnik Nov 10 '12 at 14:21
-
Sorry, I didn't notice the `CopyOnWriteArrayList` nitice. The answer was amended. – Yanick Rochon Nov 10 '12 at 14:37
I think you raised an interesting question.
I tried thinking whether ConcurrentHashMap for example, as was suggested by others can help, but I'm not sure as the lock is segment-based.
What I would do in this case , and I do hope I understood your question well, is to lock access to your collection, using a ReaderWriterLock.
The reason I chose this lock is because I do feel this needs locking (as you explained - iteration is composed from several operations) ,
And because in case of reader threads, I do not want them to wait on lock , if no writer thread is working on the collection.
Thanks to @Adam Arold I paid attention that you suggested the "synchronized decorator" - but I feel this decorator is "too strong" for your needs, as it uses a synchronized and will not diffrentiate between cases of N readers and M writers.

- 4,091
- 4
- 20
- 27
-
1Read my question about `ConcurrentHashMap`s: [How does ConcurrentHashMap works internally?](http://stackoverflow.com/questions/11793067/how-does-concurrenthashmap-works-internally) – Adam Arold Nov 10 '12 at 14:27
-
1@Adam Arold -thanks for pointing out this question. I learnt new things. The only problem (as I can see it)) is you're limiting him to a Map (or let's say - ConcurrentMap) interface. I would try to find a more generic solution, if possible, but you are right in other comments - it really depends on your needs. – Yair Zaslavsky Nov 10 '12 at 14:31
-
1
If you have an unencapsulated Collection
object of a non thread-safe class, it is impossible to prevent misuse of the Collection
, and thus the possibility of a ConcurrentModificationException
.
Other answers have suggested use of a thread-safe Collection
class, such as those provided by java.util.concurrent
. You should however consider encapsulating the Collection
object: have the object be private
, and have your class provide higher level abstractions (such as addPerson
and removePerson
) that manipulate the Collection
on behalf of the callers, and do not have any getter methods that return references to the Collection
. It is then fairly easy to enforce invariants on the encapsulated data (such as "every person has a non empty name") and provide thread-safety using synchronized
.

- 46,613
- 43
- 151
- 237