Memory semantics for final fields
Write final field rearrangement rules : Prohibit the final field of the write to be sorted out of the construction method, mainly including two aspects
1) JMM prohibit the compiler from sorting the final field to the construction method
2) The compiler will insert a storestore barrier before the constructor return after the final field is written, which prohibits the processor from sorting the final field to the constructor
The collation of the final field ensures that the final domain of the object is already being used until the object reference is visible to any thread.
Properly initialized, and the normal domain does not have this guarantee
read final field rearrangement rule: in one thread, the first read object reference and the first read of the object contains the final domain, the JMM processor prohibits the rearrangement of these two operations. The compiler will insert the Loadload barrier before reading final
Reading the final field's reorder rules ensures that before the final domain of an object is read, it must first be read with the final
Reference to the domain's object
Rearrangement rules for composite final fields:
Writes within a member domain of a final reference object inside a constructor, and then assigns a reference to a reference variable outside of the constructor to the constructor, which cannot be reordered between the two operations
Double check locking and deferred initialization
Volatile-based solutions
public class Safedoublecheckedlocking {
Private volatile static Instance Instance;
public static Instance getinstance () {
if (instance = = null) {
Synchronized (Safedoublecheckedlocking.class) {
if (instance = = null)
Instance = new instance (); Instance is volatile, it's no problem now.
}
}
return instance;
}
}
Class-Based initialization solutions
The JVM performs initialization of the class during the class initialization phase (after class is loaded and before it is used by the thread). During initialization of the class, the JVM acquires a lock that synchronizes the initialization of multiple threads to the same class.
Based on this feature, another thread-safe deferred initialization scheme can be implemented
public class Instancefactory {
private Static Class Instanceholder {
public static Instance Instance = new Instance ();
}
public static Instance getinstance () {
return instanceholder.instance; This will cause the Instanceholder class to be initialized
}
}
According to the Java specification, the following conditions are immediately initialized
1) T is a class, and an instance of type T is created.
2) T is a class, and a static method declared in T is called.
3) A static field declared in T is assigned a value.
4) A static field declared in T is used, and this field is not a constant field.
5) T is a top level class (see the Java Language Specification for §7.6), and an assertion statement is nested in the T
Internally is executed.
Process for class initialization
Five stages of class initialization
1) control the initialization of a class or interface through synchronization on a class object (obtaining a synchronization lock on a class object), the thread that acquires the lock waits until the current thread obtains an initialization lock for the object
2) thread executes the initialization of Class A, while thread B waits on the condition of the corresponding initialization lock
3) Thread A sets the status bit intilized and calls all condition waiting threads to wake up
4) thread B ends the initialization of the class
5) Initialization of thread C execution class
This article is from the "ITER Summary" blog, please be sure to keep this source http://5855156.blog.51cto.com/5845156/1954184
The art of Java concurrent Programming, reading notes Chapter III