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 either a primitive Array or an immutable object array. Ex. An int array or a String array.
Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.
We can use Collections. synchronizedList(List<T>) method to synchronize collections in java. The synchronizedList(List<T>) method is used to return a synchronized (thread-safe) list backed by the specified list.
In Java, each object has a lock or a monitor. This lock can be accessed by a thread. At a time only one thread can acquire this monitor or lock. Java programming language provides a keyword Synchronized' that allows us to synchronize the threads by making a block or method as Synchronized.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With