156

There seems to be a lot of different implementations and ways to generate thread-safe Sets in Java. Some examples include

1) CopyOnWriteArraySet

2) Collections.synchronizedSet(Set set)

3) ConcurrentSkipListSet

4) Collections.newSetFromMap(new ConcurrentHashMap())

5) Other Sets generated in a way similar to (4)

These examples come from Concurrency Pattern: Concurrent Set implementations in Java 6

Could someone please simply explain the differences, advantages, and disadvantage of these examples and others? I'm having trouble understanding and keeping straight everything from the Java Std Docs.

4 Answers 4

233
  1. The CopyOnWriteArraySet is a quite simple implementation - it basically has a list of elements in an array, and when changing the list, it copies the array. Iterations and other accesses which are running at this time continue with the old array, avoiding necessity of synchronization between readers and writers (though writing itself needs to be synchronized). The normally fast set operations (especially contains()) are quite slow here, as the arrays will be searched in linear time.

    Use this only for really small sets which will be read (iterated) often and changed seldom. (Swing's listener-sets would be an example, but these are not really sets, and should be only used from the EDT anyway.)

  2. Collections.synchronizedSet will simply wrap a synchronized-block around each method of the original set. You should not access the original set directly. This means that no two methods of the set can be executed concurrently (one will block until the other finishes) - this is thread-safe, but you will not have concurrency if multiple threads are using the set. If you use the iterator, you usually still need to synchronize externally to avoid ConcurrentModificationExceptions when modifying the set between iterator calls. The performance will be like the performance of the original set (but with some synchronization overhead, and blocking if used concurrently).

    Use this if you only have low concurrency, and want to be sure all changes are immediately visible to the other threads.

  3. ConcurrentSkipListSet is the concurrent SortedSet implementation, with most basic operations in O(log n). It allows concurrent adding/removing and reading/iteration, where iteration may or may not tell about changes since the iterator was created. The bulk operations are simply multiple single calls, and not done atomically – other threads may observe only some of them.

    Obviously you can use this only if you have some total order on your elements. This looks like an ideal candidate for high-concurrency situations, for not-too-large sets (because of the O(log n)).

  4. For the ConcurrentHashMap (and the Set derived from it): Here most basic options are (on average, if you have a good and fast hashCode()) in O(1) (but might degenerate to O(n) when many keys have the same hash code), like for HashMap/HashSet. There is a limited concurrency for writing (the table is partitioned, and write access will be synchronized on the needed partition), while read access is fully concurrent to itself and the writing threads (but might not yet see the results of the changes currently being written). The iterator may or may not see changes since it was created, and bulk operations are not atomic. Resizing is slow (as for HashMap/HashSet), thus you should try to avoid this by estimating the needed size on creation (and using about 1/3 more of that, as it resizes when 3/4 full).

    Use this when you have large sets, a good (and fast) hash function and can estimate the set size and needed concurrency before creating the map.

  5. Are there other concurrent map implementations one could use here?

11
  • 1
    Just a sight correction in 1), the process of copying data into the new array must be locked down by synchronizing. Therefore, CopyOnWriteArraySet doesn't totally avoid the necessity of synchronization. Jun 5, 2015 at 13:07
  • On ConcurrentHashMap based set, "thus try to avoid this by estimating the needed size on creation." The size you give to the map should be over 33% larger than your estimate (or known value), since the set resizes at 75% load. I use expectedSize + 4 / 3 + 1
    – Daren
    Oct 30, 2015 at 12:21
  • @Daren I guess the first + is meant to be a *? Oct 30, 2015 at 21:00
  • @PaŭloEbermann Of course... it should be expectedSize * 4 / 3 + 1
    – Daren
    Nov 19, 2015 at 14:11
  • 1
    For ConcurrentMap (or HashMap) in Java 8 if number of entries mapping to same bucket reaches threshold value (i believe it is 16) then the list gets changed to a binary search tree (red-black tree to be precised) and in that case look up time would be O(lg n) and not O(n). Dec 28, 2017 at 4:58
21

It is possible to combine the contains() performance of HashSet with the concurrency-related properties of CopyOnWriteArraySet by using the AtomicReference<Set> and replacing the whole set on each modification.

The implementation sketch:

public abstract class CopyOnWriteSet<E> implements Set<E> {

    private final AtomicReference<Set<E>> ref;

    protected CopyOnWriteSet( Collection<? extends E> c ) {
        ref = new AtomicReference<Set<E>>( new HashSet<E>( c ) );
    }

    @Override
    public boolean contains( Object o ) {
        return ref.get().contains( o );
    }

    @Override
    public boolean add( E e ) {
        while ( true ) {
            Set<E> current = ref.get();
            if ( current.contains( e ) ) {
                return false;
            }
            Set<E> modified = new HashSet<E>( current );
            modified.add( e );
            if ( ref.compareAndSet( current, modified ) ) {
                return true;
            }
        }
    }

    @Override
    public boolean remove( Object o ) {
        while ( true ) {
            Set<E> current = ref.get();
            if ( !current.contains( o ) ) {
                return false;
            }
            Set<E> modified = new HashSet<E>( current );
            modified.remove( o );
            if ( ref.compareAndSet( current, modified ) ) {
                return true;
            }
        }
    }

}
5
  • Actually AtomicReference marks the value volatile. It means it makes sure no thread is reading out stale data and provides happens-before guarantee as code cannot be reordered by the compiler. But If only get/set methods of AtomicReference are used then we are actually marking our variable volatile in a fancy way. Dec 28, 2017 at 5:05
  • This answer can't be upvoted enough because (1) unless I missed something, it will work for all collection types (2) none of the other classes provide a way to atomically update the entire collection at once... This is very useful.
    – Gili
    Aug 6, 2018 at 17:44
  • I tried appropriating this verbatim but found it was labeled abstract, seemingly to avoid having to write several of the methods. I set about adding them, but ran into a roadblock with iterator(). I don't know how to maintain an iterator over this thing without breaking the model. Seems I always have to go through the ref, and might get a different underlying set each time, which requires getting a new iterator on the underlying set, which is useless to me, as it will start with item zero. Any insights?
    – nclark
    Sep 26, 2019 at 14:14
  • Okay I guess the guarantee is, each customer getting a fixed snapshot in time, so the underlying collection's iterator would work fine if that's all you need. My use case is to allow competing threads to "claim" individual resources in it, and it won't work if they have different versions of the set. On second though... I guess my thread just needs to get a new iterator and try again if CopyOnWriteSet.remove(chosen_item) returns false... Which it would have to do regardless :)
    – nclark
    Sep 26, 2019 at 14:33
  • Is while ( true ) necessary here? Sep 10, 2020 at 16:00
13

If the Javadocs don't help, you probably should just find a book or article to read about data structures. At a glance:

  • CopyOnWriteArraySet makes a new copy of the underlying array every time you mutate the collection, so writes are slow and Iterators are fast and consistent.
  • Collections.synchronizedSet() uses old-school synchronized method calls to make a Set threadsafe. This would be a low-performing version.
  • ConcurrentSkipListSet offers performant writes with inconsistent batch operations (addAll, removeAll, etc.) and Iterators.
  • Collections.newSetFromMap(new ConcurrentHashMap()) has the semantics of ConcurrentHashMap, which I believe isn't necessarily optimized for reads or writes, but like ConcurrentSkipListSet, has inconsistent batch operations.
2
0

Concurrent set of weak references

Another twist is a thread-safe set of weak references.

Such a set is handy for tracking subscribers in a pub-sub scenario. When a subscriber is going out of scope in other places, and therefore headed towards becoming a candidate for garbage-collection, the subscriber need not be bothered with gracefully unsubscribing. The weak reference allows the subscriber to complete its transition to being a candidate for garbage-collection. When the garbage is eventually collected, the entry in the set is removed.

While no such set is directly provided with the bundled classes, you can create one with a few calls.

First we start with making a Set of weak references by leveraging the WeakHashMap class. This is shown in the class documentation for Collections.newSetFromMap.

Set< YourClassGoesHere > weakHashSet = 
    Collections
    .newSetFromMap(
        new WeakHashMap< YourClassGoesHere , Boolean >()
    )
;

The Value of the map, Boolean, is irrelevant here as the Key of the map makes up our Set.

In a scenario such as pub-sub, we need thread-safety if the subscribers and publishers are operating on separate threads (quite likely the case).

Go one step further by wrapping as a synchronized set to make this set thread-safe. Feed into a call to Collections.synchronizedSet.

this.subscribers =
        Collections.synchronizedSet(
                Collections.newSetFromMap(
                        new WeakHashMap <>()  // Parameterized types `< YourClassGoesHere , Boolean >` are inferred, no need to specify.
                )
        );

Now we can add and remove subscribers from our resulting Set. And any “disappearing” subscribers will eventually be automatically removed after garbage-collection executes. When this execution happens depends on your JVM’s garbage-collector implementation, and depends on the runtime situation at the moment. For discussion and example of when and how the underlying WeakHashMap clears the expired entries, see this Question, *Is WeakHashMap ever-growing, or does it clear out the garbage keys? *.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.