Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 812
rreyes1979 Avatar asked May 19 '11 15:05

rreyes1979


People also ask

What is meant by thread synchronization in Java programming why it is important?

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.

How do you synchronize an array?

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.

Does Java support thread synchronization?

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.


1 Answers

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.

like image 69
Brett Kail Avatar answered Sep 24 '22 14:09

Brett Kail