Compared to the locks and volatile described earlier, reading and writing to the final field is more like a normal variable access. For final domains, compilers and processors are subject to two reordering rules:
The writing of a final field within a constructor is not reordered between the two operations, and the subsequent assignment of the referenced object to a reference variable.
First read a reference to an object that contains the final domain, and the final field, which is then first read, cannot be reordered between the two operations.
Below, we illustrate these two rules separately with some sample code:
public class Finalexample {
int i; Ordinary variable
final int J; Final variable
static finalexample obj;
public void Finalexample () { //constructor
i = 1; Write general domain
j = 2; Write final domain
} public
static void writer () { //write thread a executes
obj = new Finalexample ();
}
public static void Reader () { //Read thread B executes
finalexample object = obj; Read object reference
int a = OBJECT.I; Read normal field
int b = OBJECT.J; Read final field
}
}
This assumes that a thread a executes the writer () method, and then another thread B executes the reader () method. Here we illustrate these two rules with the interaction of these two threads.
Write a reordering rule for the final field
Writing the final field's reordering rule prohibits the final field from being reordered to a constructor. The implementation of this rule consists of the following 2 aspects:
JMM prevents the compiler from sorting the final domain's write reordering beyond the constructor.
After the final field is written, the compiler inserts a storestore barrier before the constructor return. This barrier prevents the processor from sorting the final domain's write reordering into the constructor.
Now let's analyze the writer () method. The writer () method contains only one line of code: Finalexample = new Finalexample (). This line of code contains two steps:
Construct an object of finalexample type;
Assign the reference of this object to the reference variable obj.
Assuming that there is no reordering between the thread B read object references and the member fields of the Read object (which will immediately explain why this assumption is required), the following figure is a possible execution sequence:
In the above illustration, the operation of the Write normal field is sorted out of the constructor by the compiler, and read thread B incorrectly reads the value before the normal variable i initialization. While writing the final domain, the re-collation of the final field is "qualified" within the constructor, and read thread B correctly reads the value after the final variable initialization.
Writing the final field's reordering rule ensures that the final domain of an object has been correctly initialized before the object reference is visible to any thread, and the normal domain does not have this protection. For example, when reading thread B "see" Objects referencing obj, it is possible that the Obj object has not yet been constructed (the write operation for normal domain i is reordered to the constructor, at which point the initial value 2 has not yet been written to the normal domain i).