Java FAQ _07JVM Architecture (001) _java What is the memory model

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What is a memory model

The Java platform automates the integration of threading and multiprocessor technology, which is much more integrated than the computer language that Java was born before. Java for a variety of heterogeneous platform independence, so that multithreading technology also has a pioneering side.
Sometimes in Java development, a concept that is often confusing is the memory model for programs that are very demanding in terms of synchronization and thread safety. So what exactly is a memory model?
The memory model describes the relationships between variables (instance fields, static fields, and array elements) in the program, as well as the underlying details of storing variables into memory and removing variables from memory in the actual computer system.
The Java object is ultimately stored in memory, which is true, but the compiler, runtime, processor, or system cache has permission to specify the memory location to store or take out the value of the variable.

2. What are the rules of the memory model

The memory model needs to have the following rules: Atomicity (atomicity), Visibility (Visibility), sortable (ordering)
1. atomicity (atomicity)
Atomicity refers to atomic-level operations, such as the smallest piece of memory that reads and writes, and is understood to be the most basic operating unit in the Java language that is closest to memory. The data unit for this read-write operation is not the value of the variable, but the native code.

Atomic rules stipulate that when you access the values of any type of field in a storage cell and update operations on it, other types of fields, except the long and double types, must be guaranteed to be atomic, and these fields also include references to Object Services. That is, if you get or initialize some value (the values are written by another thread and are not written from two or more threads in the same timestamp), the atomicity of the value must be guaranteed inside the JVM.

In addition, atomic extension rules can extend to two other types based on long and double: volatile long and volatile double (volatile as Java keyword), A long type that is not declared by a volatile and a field value of type double, although not guaranteed to be atomic in its jmm, is allowed.

As long as it does not violate this rule, the JVM does not care what thread the value of the data comes from, which makes the Java language in the design of parallel operations extremely simple for multi-threaded atomic design. The final program does not have much impact, even if the developer does not consider it.

2. Visibility (Visibility)
Visibility refers to the state of a thread modification that is visible to another thread. That is, one thread modifies the result and another thread can see it immediately. For example: variables modified with volatile will have visibility. Volatile-modified variables do not allow the thread to cache and reorder internally, i.e. modify memory directly, so it is visible to other threads. But there is a problem to be aware of, and volatile can only make it visible to the content he modifies, but it does not guarantee that it is atomic. For example, volatile int a = 0, then there is an operation a++, this variable a has visibility, but a++ is still a non-atomic operation, and this operation also has a thread safety problem.

Under the constraints of visibility rules, you define the circumstances under which a thread can access or affect another thread, and read related data from a visible area of another thread and write data to another thread.

3. Sortable (ordering)
Sortable means that in order to improve performance, the compiler and the processor may reorder the instructions, including:

    • Compiler-Optimized reordering
      The compiler can reschedule the execution of a statement without changing the semantics of the single-threaded procedure.

    • Reordering of instruction-level parallelism
      Modern processors use instruction-level parallel technology (INSTRUCTION-LEVELPARALLELISM,ILP) to overlap multiple instructions. If there is no data dependency, the processor can change the order in which the statement corresponds to the machine instruction execution.

    • Re-ordering of memory systems
      Because the processor uses the cache and read/write buffers, this makes the load and storage operations appear to be in a disorderly sequence.

Volatile-modified variables do not allow the thread to cache and reorder internally.
The collation rules will constrain the order of any thread that violates the rule invocation in the course of the operation, and the sorting problem revolves around the sequence of reading, writing, and assignment statements.

3. What is the Java memory model

JMM (the Java memory model, the abbreviation for Java storage models) is a protocol that controls the communication between Java threads, threads, and main memory.
JMM defines the abstract relationship between the thread and the main memory: Shared variables between threads are stored in main memory, and each thread has a private local memory (local memory), where the thread is stored in a copy of the read/write shared variable. Local memory is an abstract concept of JMM and is not really there. It covers caching, write buffers, registers, and other hardware and compiler optimizations.
?? The JMM structure is as follows:

Java FAQ _07JVM Architecture (001) _java What is the 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.