Java memory model

Source: Internet
Author: User

1.happens-before Introduction Starting with JDK 5, Java uses the new JSR-133 memory model (unless otherwise noted, this article is for the JSR-133 memory model). JSR-133 uses the concept of Happens-before to illustrate the memory visibility between operations. In JMM, if the result of one operation needs to be visible to another operation, there must be a happens-before relationship between the two operations. The two actions mentioned here can be either within a thread or between different threads. The Happens-before rules that are closely related to programmers are as follows. • Program Order rules: Each action in a thread is happens-before by any subsequent action in that thread. • Monitor lock rule: unlock a lock, Happens-before to the locking of the lock. volatile variable rule: writes to a volatile field, happens-before to any subsequent reading of the volatile field. • Transitivity: If a happens-before b, and B happens-before C, then a Happens-before c. Note the Happens-before relationship between the two operations does not mean that the previous operation must be performed before the next operation, and reordering (including compiler reordering and handler reordering) can disrupt the order of execution! Happens-before only requires that the previous operation (the result of the execution) be visible to the latter operation, and that the previous operation precedes the second operation in order. 1.1. Data dependencies: When the compiler and the processor are reordered, the data dependencies are respected, and the compiler and processor do not change the order in which the two operations exist that have data dependencies. As long as the order of execution of the two operations is reordered, the execution result of the program is changed. 1.2 As-if-serial semantics: No matter how reordering (compilers and processors in order to improve parallelism), the execution results of (single-threaded) programs cannot be changed. The compiler, runtime, and processor must adhere to as-if-serial semantics.     1.3 Sequential consistent memory model: a theoretical reference model, in which the memory model of the processor and the memory model of the programming language are referenced in a sequential consistent memory model. 1) All operations in a thread must be executed in the order of the program.2) (regardless of whether the program is synchronized) all threads can see only a single sequence of operations execution. 1.4JMM The following guarantees the memory consistency of the correctly synchronized multithreaded threads. If the program is correctly synchronized (synchronized, volatile, and final correct use), the execution of the program will have sequential consistency-that is, the execution result of the program is the same as the execution result of the program in the sequential consistent memory model. 1.5final Domain Reorder Rules: 1) writes to a final field within the constructor, and then assigns a reference to the constructed object to a reference variable, which cannot be reordered between the two operations. 2) The first time you read a reference to an object containing a final field, and the final field is then first read, the two operations cannot be reordered. These two points guarantee that the reference to the object containing the final field must be read before the final domain of the object is read, and that the JMM minimum security model only guarantees that the referenced variable is initialized to NULL 0 false. 1.6 Double check lock is still unsafe

Memory = allocate (); 1: Allocating the object's memory space ctorinstance;//2: Initialize Objectinstance = memory;///3: Set instance to point to the memory address just allocatedSteps 2 and 3 above may be reordered by adding the volatile keyword before the instance variable, preventing steps 2 and 3 from reordering or based on class initialization solutions. The JVM performs initialization of the class at the initialization stage of the class (that is, after class is loaded and before it is used by the thread). During initialization of the class, the JVM goes to fetch a lock (class object). This lock synchronizes the initialization of multiple threads to the same class.

If you do need to use thread-safe deferred Initialization for instance fields, use the volatile-based deferred initialization scenario described above, and if you do need to use thread-safe deferred initialization for static fields, use the class-based initialization scenario described above. 1.6JMM of memory visibility guaranteed by program type, the memory visibility of Java programs can be classified into the following 3 classes. Single threaded. There is no memory visibility issue with single threaded threads. The compiler, runtime, and processor work together to ensure that the results of a single-threaded program are the same as the execution of the procedure in the sequential consistency model. A multithreaded program that synchronizes correctly. The execution of multithreaded programs that are correctly synchronized will have sequential consistency (the execution result of the program is the same as that of the program in the sequential-consistent memory model). This is the focus of JMM's attention, and JMM provides the programmer with the assurance of memory visibility by restricting the reordering of compilers and processors. • Multithreaded programs that are not synchronized/not synced correctly. JMM provides them with minimal security: The value read when the thread executes, either the value written by a previous thread or the default value (0, NULL, FALSE).

Java memory model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.