Java thread safety of list

前端 未结 4 1004
一个人的身影
一个人的身影 2021-02-05 19:00

I have a List, which is to be used either in thread-safe context or in a non thread-safe one. Which one will it be, is impossible to determine in advance.

In such specia

4条回答
  •  时光取名叫无心
    2021-02-05 19:54

    If the traverses happens more often than writing I'd look into CopyOnWriteArrayList.

    A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

提交回复
热议问题