In Java, is it required to synchronize write access to an array if each thread writes to a separate cell space?

前端 未结 10 1771
北恋
北恋 2020-12-15 17:14

Is it required to synchronize write access to an array in Java if each thread writes to a separate cell space?

EDIT: Specifically, the array is eith

10条回答
  •  半阙折子戏
    2020-12-15 17:27

    Not a simple yes or no issue. Some things to consider:

    • Your question implies that you are storing primitive types (or references) in your array. Performing complex operations on objects stored in the array may require synchronization.
    • Changing long or double values is not safe without synchronization
    • Even if you are storing primitives that are not double or long, there is the possibility of another threads not being able to see changed values immediately because of caching (resulting in stale reads)

提交回复
热议问题