Java thread safety of list

前端 未结 4 1000
一个人的身影
一个人的身影 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:32

    Take a look at how AtomicInteger (and the likes) are implemented to be thread safe & not synchronized. The mechanism does not introduce synchronization, but if one is needed, it handles it gracefully.

提交回复
热议问题