Is it thread-safe for java to write multiple threads to different parts of the same array?

for example, I have an int array of 10 elements, thread A modifies the element with subscript 0, and thread B modifies the element with subscript 5.
does this cause thread safety problems?

personally, I don"t think there will be a problem, but it is a dangerous operation.

Jul.13,2022

Thread-safe corresponds to data contention. Officially, data contention that is not sorted by happens-Before relations will cause asynchrony.
since you are operating on different parts of an array, multiple threads do not read and write to the same array element at the same time (that is, one thread reads and the other thread writes at the same time), and there is no data race even if it is not synchronized correctly, so it is thread-safe.


of course, but you need to add synchronization when threads access each element to prevent multiple threads from manipulating the same element, as ConcurrentHashMap does.


JVM spec requires atomic reading and writing of int and references (not required by long and double)

but what is "thread safe" or "dangerous"?

Menu