0

In jls-17.7 it is mentioned:

... a single write to a non-volatile long or double value is treated as two separate writes: one to each 32-bit half...

And then it says,

Writes to and reads of references are always atomic, regardless of whether they are implemented as 32-bit or 64-bit values.

I want to understand what this second paragraph means. It is confusing me because in the first paragraph, it is mentioned that a write to long and doubles are treated as two separate writes, and in this quote they are contradicting it.

Please note that I have already read this but this doesn't answer the explanation I am looking for.


UPDATE: I received a comment - "references are not longs or doubles, they are treated differently, so there is no contradiction"...

That's what is confusing me, we do operation using references only, right? For example: long l1 = 88;. Then what's the meaning of that line.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
pjj
  • 2,015
  • 1
  • 17
  • 42
  • 2
    references are not longs or doubles, they are treated differently, so there is no contradiction – Iłya Bursov Feb 04 '22 at 20:37
  • 2
    Also note the paragraph after that: _an implementation of the Java Virtual Machine is free to perform writes to long and double values atomically or in two parts_. That means that the actual JVM implementation **may** read and write long and double value atomically or not at its discretion, but for references it **must** read and write them atomically. – Thomas Kläger Feb 04 '22 at 20:46
  • @IłyaBursov please see my update. – pjj Feb 04 '22 at 20:49
  • reading partially written long/double does not lead to memory errors, while reading partially updated references - breaks memory security – Iłya Bursov Feb 04 '22 at 20:55
  • Do you understand that to "read a reference" is to determine what object the reference refers to and that to "write a reference" is to change what object the reference refers to? Maybe not quite understanding that is an issue. – David Schwartz Feb 04 '22 at 21:10
  • When you assign references to each other, they are atomic. This means that is is not possible to get a corrupted reference during the assignment phase. For `Object a` and `Object b`, `a = b;` is atomic. For `long a` and `long b`, `a = b;` may not be atomic. – WJS Feb 04 '22 at 21:35
  • 1
    _we do operation using references only_ that's wrong. [JLS-4.1 The Kinds of Types and Values](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1) distinguishes between primitive types (`byte`, `int`, `long`, `double` and some more) and reference types. – Thomas Kläger Feb 04 '22 at 22:00

1 Answers1

3

Writes to and reads of references are always atomic...

Suppose there is a variable SomeType v. "...Always atomic" means that if some thread T accesses v, it is guaranteed either to get the initial value of v, or else it will get a valid reference to a SomeType object or a NULL reference that the program assigned to v. The thread will never see an invalid reference, or an object reference that was not assigned to the variable.

Variables of type long and double are different. If Thread T writes a new value to some double d, and thread U accesses d, then it is possible for thread U to see a value that is different from any value that was ever assigned to d. Same goes for long.


Update

Iłya Bursov commented, "references are not longs or doubles, they are treated differently..." and then you replied:

That's what is confusing me, we do operation using references only, right?

No. Java has several primitive data types; long, double, int, boolean, etc. A variable with a primitive type is not a reference to an object. It simply holds the value. All of the primitive values fit into 64 bits or less. And what the JLS is telling you is that the rule for the two 64-bit primitives—the rule for long and for double—is less strict that the rule for any other primitive type, and it is less strict that the rule for references—even when a reference also is 64 bits.

The JLS requires all variable assignments to be atomic except for assignments to long and to double variables.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57