Search code examples
javajava-memory-model

Read and Write Long and Double local variables in Java


It is known that "reads of/writes to" non-volatile long and double variables may be non-atomic. This concerns static and non static fields and arrays.

And what about corresponding long/double local variables. Do they atomic or not.


Solution

  • Primitive values are never stored on the heap if they appear as a local variable. This means, they cannot be shared as they are stored on the stack and are not accessible to any other thread. From within this thread, you are guaranteed sequential consistency, meaning that you will always see the last assigned value.