JDK5 introduces the new specification of JMM: JSR-133, introduces the concepts of happens-before/visibility, and defines the semantics of keywords such as synchronized/volatile/final. Solved the thread security issue of final variable initialization in the constructor and the re-Sorting Problem between volatile and no-volatile variables. Why does Memory Model need to read this variable after thread1 modifies a variable in a multi-thread scenario, in this case, the command execution sequence may occur (because the compiler optimizes the command, the processor rearranges the command, and the write data cache is not updated to the main memory in time ). How can we ensure that the variable to be read by thread2 is the variable to be modified by thread1? Memory Model describes the variables in the program and Their Relationships in registers, caches, and Memory. That is, the order of variable calls in a multi-threaded environment cannot be guaranteed without the Momory Model, with the semantic definitions corresponding to the specific keywords of the Memory Model (such as synchronized/volatile/final), you can use these keywords to ensure that the program is correctly executed in a multi-threaded environment according to the desired logic. Old JMM problems #1: Immutable objects are not Immutable objects: all fields of objects must be final, it must be a primitive type or an immutable object reference. For example, the String object is unchangeable and synchronization is not required in semantics. However, in fact, there may be competition conditions in multi-threaded scenarios. @ JKD1.4class String {static final char [] charArray; static int length; static int offset; // indicates the start position of the String} The charArray and length and offset can be shared among multiple strings/StringBuffer. For example, String. substring () is the charArray that shares the original String. String s1 = "/usr/tmp"; String s2 = s1.substring (4); // contains "/tmp" in the old JMM, there is no synchronization. When initializing s1, the object constructor initializes the length/offset value to 0. At this time, other threads can access these values (this is obviously problematic when substring is used ), next, the String constructor assigns a value for length/offset to the required value. The new JMM model solves the thread security issue when final variables are initialized in the constructor. That is, final variables are invisible to other threads before initialization (before the constructor is executed. Question #2: the volatile variable and the non-volatile variable reordering volatile variable have only one semantics in the old JMM: for reading a volatile variable, you can always see the Final write of this volatile variable by any other thread. That is, write happends-before to the volatile variable and other threads read it. In the old JMM, although the volatile variables are not allowed to be reordered, the volatile variables and other common variables are allowed to be reordered. This causes problems when the volatile variable is used as the flag. Copy the code Map configOptions; char [] configText; volatile boolean initialized = false ;... // In thread AconfigOptions = new HashMap (); configText = readConfigFile (fileName); processConfigOptions (configText, configOptions); initialized = true ;... // In thread Bwhile (! Initialized) sleep (); // use configOptions. In the old JMM, configOptions cannot be initialized because the variable order may have been rearranged. The new JMM model of the copied code solves this problem and introduces a new syntax: volatile variables cannot be reshuffled with non-volatile variables. New JMM Visibility when ThreadA executes val = 1, how can other threads ensure that the execution result of ThreadA is visible (that is, the Visibility of variables on other threads at this time )? Some definitions in the JMM specification (volatile/synchronized/final) to ensure this. Synchronized and visibility synchronized not only have the semantics of locking into a critical section, but also have the memory visibility (memory visibility. When you leave the synchronized block, the cache must be flushed to the primary memory. When you enter the synchronized block, the cache becomes invalid and subsequent read operations must be performed in the primary memory. That is, the variable write operation in the synchronized block is visible to other threads! The new JMM of Volatile enhances the memory semantics of volatile and prohibits the shuffling of common variables. That is, threadA writes a volatile variable, and threadB reads that variable. All variables visible to A must be visible to B at the same time. Happens-before common multithreading without any data sharing and interaction, the command may be rearranged due to optimization without affecting it. If there is data competition between threads, you need to use synchronized (Moniter)/volatile to ensure the execution sequence of commands. JMM defines a sort called "happens-before". With this concept, JMM explains what memory visibility is. If the result of one operation A is visible to the memory of another operation B, then A happends-before B. * In a thread, the previous operation is happends-before. * Unlock happends-before of monitor subsequent lock operations. * Write operation for volatile variables happends-before subsequent reads. * The call operation of Thread. start () is the internal operation of the happends-before Thread. * All operations of the thread are performed by the happends-before subsequent thread join (). Data races-there is Data competition in Data competition, indicating that the program is not well synchronized and the relationship between happends-before is not well processed. The final variable initialization security object is visible to other threads only after full initialization. This shows that the initialization of final variables can achieve thread security without using synchronization. In this way, the final variable implements final (that is, no-can-change) in the true sense, and does not seem to change during the initialization process or after multithreading. Copy the Code class A {final Map <String, String> map = null; public void A () {map = new HashMap <String, String> (); map. put ("key1", "value1") ;}} copy the code. before other threads reference the object of A, JMM ensures that the final variable of A has been fully initialized by its constructor. That is to say, the final variable fully initializes the reference of other threads of happends-before to this object. That is, the initialization of final variables in the constructor is thread-safe.