views:

41

answers:

1

What is the difference between CopyOnWritearraylist and Collections.synchronizedList(..)? When should one be preferred over the other.

+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