When it's applicable to use CopyOnWriteArrayList

This question already has an answer here:

  • In what situations is the CopyOnWriteArrayList suitable? [duplicate] 2 answers

  • Based on the javadoc,

    A thread-safe variant of java.util.ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

    As you can see, any modification to the container is costly especially when the size of data in the container is big.

    It's better to use CopyOnWriteArrayList when the frequency of modification is low. As suggested by JCIP ,

    the copy-on-write collections are reasonable to use only when iteration is far more common than modification.

    For example the event-notification system. It will iterate the container every time an event needs to be notified but will only be modified when the observer is changed. The occurrence of the former is with high frequency and the latter is with low frequency.

    链接地址: http://www.djcxy.com/p/76122.html

    上一篇: 玩! 框架使用一个<lot>的静态

    下一篇: 适用于使用CopyOnWriteArrayList时