Is the Java memory model a guarantee of visibility of in-threaded records?

Consider a simple single-threaded execution of a Java program in which no synchronization actions are involved, just read and write instance variables. An implementation that simply ignores all entries appears to comply with the Java Memory Specification. First, the applicable general statement from Β§17.4 :

The memory model determines which values ​​can be read at each point in the program. The actions of each thread in isolation should behave as defined by the semantics of that thread, except that the values ​​observed by each read are determined by the memory model.

The relevant restrictions will be as follows ( Β§17.4.5 ):

1. occurs - before the ordering caused by the order of the program:

If x and y are actions of the same thread, and x is up to y in program order, then hb (x, y).

2. It happens - until consistency:

A set of actions is performed A to the agreed one, if for all it reads r in A, where W (r) is the write action observed by r, then there is no need for either hb (r, W (r)), or that exists we write w in such that wv = rv and hb (W (r), w) and hb (w, r).

This basically prevents reading what is happening - before he writes. Another condition is simply a condition of sanity that prevents the reading of some v, seeing an earlier record of it v, which in the meantime was accompanied by another record of the same v.

, , , , .

? JVM ?

+5
1

:

class MyClass {
    private static int i = 0;

    public static void main(String[] args) {
        i = 3; //w
        System.out.println(i); //r
    }
}
  • , .
  • , (Β§17.4.3).
  • (Β§17.4.1), , , .



    = > hb (w, r)
    = >
    = > .
    = > 3

+3

All Articles