Fix ConcurrentModificationException on ArrayList with CopyOnWriteArrayList
You have a List in your application that is traversed or iterated over very frequently but is modified very rarely. Like a ‘Top 10′ kind of list on your home page which is served for every hit to the home page (think hundreds of hits per second) and is updated by an hourly Quartz job.
If you use an ArrayList for that list and no synchronization, you run the risk of a ConcurrentModificationException being thrown every hour when the list is modified by the Quartz job because the list may currently be in the middle of a traversal or iteration for a user visiting your homepage.
One way a person might try to fix it is to use a Vector or rather Collections.synchronizedList(List<T>). But this doesn’t help! While add(), remove(), get() will now be thread-safe on the list, Iterators will still throw ConcurrentModificationException! You will need to synchronize on the list object before iterating on it as well as in the Quartz job where you modify it. That’s just terrible from a performance and scalability point of view! Synchronization will be needed everywhere and all the time just because of an hourly modification.
Fortunately, there is a better solution. Use a CopyOnWriteArrayList.
Whenever a structural modification is made to this list, a new copy is created. This is not inefficient if modifications happen rarely. Iterators on this will not experience a ConcurrentModification because the list they started iterating on is never modified.
Another way one may try to avoid Concurrent Modification without adding synchronization code all over is to build a new List in their scheduled job and then switch out the reference which is used for iteration with the new list. Assigning a reference is an atomic operation in the JVM. This approach would fail if the code doing the iteration used an old style for loop - for (int i=0; i<list.size(); i++) { … list.get(i) …}. A change in the size of the switched list introduces a failure point. Worse, there are visibility problems here since the changes from a different thread may not be seen at all. Using the volatile keyword may help see the changes to the reference from a different thread, but the changed list size would still be a problem.
The memory consistency and happens-before guarantees of Copy On Write Array List make these problems disappear. Also, the code is simple since one doesn’t need to use volatile or synchronized. Less code, less bugs!
Another use case for CopyOnWriteArrayList is the case of the Observer design pattern. If event listeners are added and removed by multiple threads, it is quite possible that using a CopyOnWriteArrayList might be appropriate for correctness and simplicity.
Hope this helps. Remember to get your code reviewed by your nearest concurrency guru!
Tags: Concurrency, Java, Multi-threading, Threads

I’ve a question regarding the case where event listeners are added and removed by multiple threads. If you use CopyOnWriteArrayList, what happens in the use case that previously generated the ConcurrentModificationException? You would no longer get the CME, but you would now call a listener that thought it had been removed.
Richard, that observation is correct.
A few quick thoughts -
In some (many?) cases, an extra event notification might not be a problem.
In other cases, the listener code may need to deal with the fact that it could receive a notification even if it has unregistered its interest in the event. From one point of view, this would be good design since one would reduce dependence on the event notification code to be working correctly.
Could have a volatile variable in the listener that maintains the state of the listener.
If listener events are processed asynchronously, stop the event processor before unregistering the listener.
Guarantees and concurrency are not friends!
jau uzkniso karstas komentaras,po debiliskai nesamoninga rasliavele kaskart pameta,o visi net velykas uzmirse puola kaip suo kaula ir urzgia.nekirsykit zmoniu bent pries didziasias sventes