In Java, do I need to declare my collection synchronized if it's read-only?

后端 未结 5 1209
挽巷
挽巷 2021-02-12 12:30

I fill a collection one single time when my J2EE webapp starts. Then, several thread may access it at same time but only to read it.

I know using a synchronized collecti

5条回答
  •  攒了一身酷
    2021-02-12 13:06

    You do not have to, as explained in other answers. If you want to ensure that your collection is read only, you can use:

    yourCollection = Collections.unmodifableCollection(yourCollection);
    

    (similar method exist for List, Set, Map and other collection types)

提交回复
热议问题