When is CopyOnWriteArraySet useful to achieve thread-safe HashSet?

前端 未结 3 1488
梦谈多话
梦谈多话 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:28

    Copy-on-write structures are functionally immutable.

    Java at one point had a very poor story for providing immutable views on writeable structures such as sets. For example, if you had a set member, and you returned it publicly, the caller could just turn around and edit it, and therefore be editing your object's internal state! But what else can you do, copy the entire thing before returning from any public function? That would be pointlessly slow.

    This was the story earlier in Java history. They relied almost exclusively on immutable objects (string is an example). Collections were an exception to this pattern, and were therefore problematic from an encapsulation perspective. When CopyOnWriteArraySet was added, unmodifiableCollection and unmodifiableSet did not yet exist (although unmodifiableCollection has largely solved the problem, I still find it a more cumbersome solution than what other languages offer, especially when using custom data structures). So this explains probably the largest motivation for creating CopyOnWriteArraySet in the first place. You could return a CopyOnWriteArraySet without fear of somebody else modifying your object's internal state, and without wasting time making unnecessary copies.

    Copy-On-Write was a fad several years ago, but it is a notoriously inefficient idea for multi-threaded programming and is less efficient than other models. From the documentation you've posted, they've sped up iterating over it by creating thread-local snapshots, which means they are spending memory to compensate. So it's a perfectly okay class to use as long as your data is small... because the memory snapshots won't add up to much wasted memory.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 15:43

    Test code:

    Set<String> a = new CopyOnWriteArraySet<String>();
        for(int i=0;i<10;i++) {
            a.add("str" + i);
        }
        boolean flag = true;
        long t1 = System.currentTimeMillis();
        for(int i=0;i<200000;i++) {
            flag = a.contains("str" + i);
        }
        System.out.println(System.currentTimeMillis() - t1);
    
        Set<String> b = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
        for(int i=0;i<10;i++) {
            b.add("str" + i);
        }
        t1 = System.currentTimeMillis();
        for(int i=0;i<200000;i++) {
            flag = b.contains("str" + i);
        }
        System.out.println(System.currentTimeMillis() - t1);
    

    Shows that CopyOnWriteArraySet is slower than Collections.newSetFromMap. Since the test case is a very small Set with read only operation, CopyOnWriteArraySet seems not better.

    0 讨论(0)
提交回复
热议问题