is Java HashSet thread-safe for read only?

Asaf Mesika picture Asaf Mesika · Mar 21, 2011 · Viewed 34.9k times · Source

If I have an instance of an HashSet after I ran it through Collections.unmodifiableSet(), is it thread-safe?

I'm asking this since Set documentation states that it's not, but I'm only performing read operations.

Answer

Brian Roach picture Brian Roach · Mar 21, 2011

From the Javadoc:

Note that this implementation is not synchronized. If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally

Reading doesn't modify a set, therefore you're fine.