2

Going through the JLS by James Gosling, I came across this-

The Java programming language provides a second mechanism (other than synchronisation), volatile fields, that is more convenient than locking for some purposes.

A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable.Then the author points to this resource.

enter image description here

It may appear that the result r2 == 2 and r1 == 1 is impossible. But why?

Isn't it makes perfect sense to think of something like this-

Instruction 4 : A = 2;
Instruction 1 : r2 = A;
Instruction 2 : B = 1;
Instruction 3 : r1 = B;

And the rest I couldn't understand it either.

It may appear that the result r2 == 2 and r1 == 1 is impossible. Intuitively, either instruction 1 or instruction 3 should come first in an execution. If instruction 1 comes first, it should not be able to see the write at instruction 4. If instruction 3 comes first, it should not be able to see the write at instruction 2.

If some execution exhibited this behavior, then we would know that instruction 4 came before instruction 1, which came before instruction 2, which came before instruction 3, which came before instruction 4. This is, on the face of it, absurd.

However, compilers are allowed to reorder the instructions in either thread, when this does not affect the execution of that thread in isolation. If instruction 1 is reordered with instruction 2, as shown in the trace in Table 17.4-B, then it is easy to see how the result r2 == 2 and r1 == 1 might occur.

Please exemplify.

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105

1 Answers1

0

This is a common rhetorical pattern in English writing. A writer says that something "may appear" to be the case or say that someone "might think" something is the case, they explain the invalid reasoning that might lead to that conclusion, and then they go on to explain how it's not in fact the case. Using this rhetorical technique, the middle section consists of statements that are known by the writer to be incorrect.

The section you are quoting is intended to explain precisely what it is that you already understand -- compilers are generally free to reorder memory reads and writes if they can't affect single-threaded code.

So for example, in this paragraph:

It may appear that the result r2 == 2 and r1 == 1 is impossible. Intuitively, either instruction 1 or instruction 3 should come first in an execution. If instruction 1 comes first, it should not be able to see the write at instruction 4. If instruction 3 comes first, it should not be able to see the write at instruction 2.

By "may appear" he means that someone might think it, but it's not true. He then goes on to explain why someone might think it and then explain why that would be wrong. The sentences after the first one in that paragraph are known to be wrong by the writer, and he will then go on to explain how they are wrong.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Not a native English speaker. For me, it may is like saying/thinking [it seems/expressing possibility]. Or putting it like this - It looks like that the result r2 == 2 and r1 == 1 is impossible, but it is achievable. – Farhan stands with Palestine Jul 28 '16 at 18:52
  • The word "appear" means "look like". It is similar to "seem". It does not suggest that it's actually possible, only that one might think it is. – David Schwartz Jul 28 '16 at 18:53