is Java HashSet thread-safe for read only?

前端 未结 7 1408
清酒与你
清酒与你 2021-02-01 16:51

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 n

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 16:58

    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.

提交回复
热议问题