When is CopyOnWriteArraySet useful to achieve thread-safe HashSet?

前端 未结 3 1487
梦谈多话
梦谈多话 2020-12-24 14:49

In Java, there is thread-safe version HashMap named ConcurrentHashMap and thread-safe version TreeMap named ConcurrentSkipListMap, but there is no Concurr

3条回答
  •  时光说笑
    2020-12-24 15:38

    It is useful when you have a small set of element for a thread safe collection.

    One example is a Set of listeners. You need to ensure uniqueness and iterate over them efficiently.

    BTW CopyOnWriteArraySet has the lowest overhead on a per reference basis. It can be as little as 1/6 the size of the other collections. This is particularly useful if you have a lot of them.

    while Set data structure is for high performance contains operation, could anybody explain this?

    COWAS is more efficient in terms of memory and it's contains is faster for small collections than the alternatives. What is "high performance" depends on the use case.

提交回复
热议问题