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

前端 未结 10 1760
北恋
北恋 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:31

    No, synchronization is not needed.

    It is defined in JLS §17.6 Word Tearing:

    One implementation consideration for Java virtual machines is that every field and array element is considered distinct; updates to one field or element must not interact with reads or updates of any other field or element. In particular, two threads that update adjacent elements of a byte array separately must not interfere or interact and do not need synchronization to ensure sequential consistency.

提交回复
热议问题