问题描述
CopyOnWritearraylist
和 Collections.synchronizedList(..)
之间有什么区别?何时应优先于其他。
What is the difference between CopyOnWritearraylist
and Collections.synchronizedList(..)
? When should one be preferred over the other.
推荐答案
CopyOnWriteArrayList
当读取数量远远超过写入数量时使用。这是因为您在每次写入时都要为昂贵的阵列复制交易进行不必要的同步。
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.
例如,当您有一个 List
在多线程环境中的事件监听器,您需要使用 CopyOnWriteArrayList
,因为
For example, when you have a List
of event listeners in a multi-threaded environment, you'd want to use CopyOnWriteArrayList
, because
- $
- 事件侦听器很少注册
这篇关于Java:CopyOnWriteArrayList vs synchronizedList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!