What is the difference between CopyOnWritearraylist
and Collections.synchronizedList(..)
? When should one be preferred over the other.
views:
41answers:
1
+1
A:
CopyOnWriteArrayList
list should be used when the number of reads vastly outnumber the number of writes. This is because you are trading unnecessary synchronization for expensive array copying on each write.
For example, when you have a List
of event listeners in a multi-threaded environment, you'd want to use CopyOnWriteArrayList
, because
- events are fired, and hence the list is iterated very often
- event listeners are registered rarely
Bozho
2010-10-01 20:00:32