Java concurrency – writing to different indexes of the same array

For two different indexes in an array the same rules apply as for two separate variables.

The Chapter “Threads and Locks” in the Java Language Specification starts by stating:

17.4.1 Shared Variables

[…]

All instance fields, static fields and array elements are stored in heap memory. In this chapter, we use the term variable to refer to both fields and array elements.

This means that you can safely write to two different indexes concurrently. However you need to synchronize a write/read to the same index if you want to make sure the consumer thread sees the last value written by the producer thread.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)