How to Synchronize HashSet in Java

This post shows how to synchronize HashSet in Java and the HashSet’s thread safe alternative which can be used instead.

HashSet is not thread safe

HashSet in Java is not thread safe as it is not synchronized by default. If you are using HashSet in a multi-threaded environment where it is accessed by multiple threads concurrently and structurally modified too by even a single thread then it must be synchronized externally. A structural modification is defined as any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.

Options for thread-safe Set

If you want to synchronize HashSet in Java or looking for a thread-safe alternative of HashSet then there are following options.

  1. Use Collections.synchronizedSet() to synchronize Set– This method returns a synchronized (thread-safe) set backed by the specified set.
  2. Using CopyOnWriteArraySet– Another option is to use CopyOnWriteArraySet from the java.util.concurrent package which is the thread safe implementation of the Set. CopyOnWriteArraySet uses an internal CopyOnWriteArrayList for all of its operations. For all mutative operations (add, set, remove, etc.) a new copy is created that is why using it is expensive.

Using Collections.synchronizedSet()

You can synchronize HashSet by using Collections.synchronizedSet() method. First we’ll see an example what happens if HashSet is used in a multi-threaded environment without synchronizing it.

Java program

In the Java code four threads are created, each of these thread adds 5 elements to the Set. After all the threads are done Set size should be 20.

Output

In one of the run size was 19, in another run 18 and sometimes even 20, so you can see that thread interference is making the behavior unpredictable. So we’ll synchronize the HashSet using the same example.

Output

Now every time size of HashSet is 20.

Using CopyOnWriteArraySet

Another option to have a thread safe Set is to use CopyOnWriteArraySet. Let’s see it using a simple example where a CopyOnWriteArraySet is created and then iterated. While iteration an element is removed using the Set’s remove method, it still won’t throw ConcurrentModificationException. In the output you can see iteration is displaying all the elements as iteration is done on a separate copy of CopyOnWriteArraySet.

Output

That’s all for the topic How to Synchronize HashSet in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.