Synchronised Collections Vs CopyOnWriteArray
Synchronised collections does not provide threadsafe when iterating over the array
It is imperative that the user manually synchronize on the returned list when iterating over it:
List list = Collections.synchronizedList(new ArrayList());
...
synchronized (list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
Note:
- This will block the other operations as well on interacting with the list
Wheras CopyOnWriteArrayList create a snapshot to modify the list, so the iteration through the list does not required to be synchronised.