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

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


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.


If read access is also partitioned in the same manner, then synchronization is not needed as per bkail's link.

But if the threads read each other's writes, it would still be necessary to have a memory barrier to force synchronization of cache contents. Otherwise, threadys may get inconsistent read results.


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)