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

前端 未结 9 772
小鲜肉
小鲜肉 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:23

    This is neccessary if:

    1. There is still a reference to the original modifiable list.
    2. The list will possibly be accessed though an iterator.

    If you intend to read from the ArrayList by index only you could assume this is thread-safe.

    When in doubt, chose the synchronized wrapper.

提交回复
热议问题