Does the unmodifiable wrapper for java collections make them thread safe?

前端 未结 9 725
小鲜肉
小鲜肉 2020-12-30 02:10

I need to make an ArrayList of ArrayLists thread safe. I also cannot have the client making changes to the collection. Will the unmodifiable wrapper make it thread safe or d

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 02:34

    From looking at the Collections source, it looks like Unmodifiable does not make it synchronized.

    static class UnmodifiableSet extends UnmodifiableCollection
                     implements Set, Serializable;
    
    static class UnmodifiableCollection implements Collection, Serializable;
    

    the synchronized class wrappers have a mutex object in them to do the synchronized parts, so looks like you need to use both to get both. Or roll your own!

提交回复
热议问题