is Java HashSet thread-safe for read only?

前端 未结 7 1409
清酒与你
清酒与你 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:57

    Yes, it is safe for concurrent read access. Here is the relevant sentence from the documentation:

    If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.

    It states that you only need to synchronize if at least one thread modifies it.

提交回复
热议问题