开发者

Java: CopyOnWriteArrayList vs synchronizedList

开发者 https://www.devze.com 2023-01-18 05:11 出处:网络
What is the difference between CopyOnWritearraylist and Collections.开发者_开发百科synchronizedList(..)? When should one be preferred over the other.CopyOnWriteArrayList list should be used when the n

What is the difference between CopyOnWritearraylist and Collections.开发者_开发百科synchronizedList(..)? When should one be preferred over the other.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消