0

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.

Vitaly
  • 845
  • 1
  • 10
  • 29
  • 1
    Local variable is not going to be shared across the multiple thread. – Snehal Patel Feb 21 '17 at 13:03
  • If they are local, why do you care about atomicity? Local variables cannot be accessed by other threads. – sparik Feb 21 '17 at 13:04
  • In general, you get problems because reading writing to long and double is not atomic. If you use local variables that are only accessed by one thread, it should not be a problem. Maybe duplicate: http://stackoverflow.com/questions/517532/writing-long-and-double-is-not-atomic-in-java – Supahupe Feb 21 '17 at 13:04
  • If by local you mean local to a method or initializer (which would the standard meaning), there is no issue since, as said already, there is no way that two threads could access the same variable. – Ole V.V. Feb 21 '17 at 13:22
  • Yes they are not shared, but this action can be Thread Divergence Action as mentioned in memory model and hence concerns JMM spec – Vitaly Feb 21 '17 at 13:27
  • I fail to see how a Thread Divergence Action can be relevant for the atomicity of reading and writing longs or doubles. A Thread Divergence Action can in some cases cause other threads to hang, but if so, this will be the case no matter if the read or write of the long or double is atomic or not. – Ole V.V. Feb 21 '17 at 15:44
  • Hence, you mean that this problem is not reflected in official documentation as this is not significant, and is up to the concrete implementation of JVM – Vitaly Feb 21 '17 at 17:38

1 Answers1

1

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.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192