The historical changes of the Java memory model

Source: Internet
Author: User

Transferred from: Http://www.csdn.net/article/2015-05-20/2824722-Java

Abstract: This paper presents the historical changes of Java technology by introducing the new/old memory model of Java.

This paper presents the historical changes of Java technology by introducing the new/old memory model of Java.

The old Java memory model

Java uses a concurrent model of shared memory that shares variables between threads. The Java language defines the threading model specification, which controls the interaction of threads and variables through a memory model, thus enabling communication between Java threads. Before JDK5, Java had been using the old memory model. As shown in 1. Variables are stored in main memory shared by all threads, and variables in main memory are called Mastingcopy. Each thread has a working memory, which holds the workingcopy of the variable. The old memory model defines a number of rules that ensure when a thread transfers mastingcopy in main memory to the working memory of the thread, and when the thread transfers the workingcopy in the working memory back to the main memory. The old memory model uses 8 operations to define the actions that a thread can perform.

    • Read operation: Main memory transmits mastingcopy to the working memory of the thread for use by subsequent load operations.
    • Load operation: The thread will put the value transferred from main memory by the read operation into the working memory.
    • Use action: A thread transfers the workingcopy of a variable to the thread execution engine.
    • Assign (Assignment) action: A thread transfers the value of a variable from the thread execution engine to the working memory of the thread.
    • Store operation: A thread transfers the workingcopy of a variable to the main memory for use by subsequent write operations.
    • Write operation: The main memory is placed in the main memory by the value transmitted by the store operation.
    • Lock (Lock) Action: The thread obtains the lock of the specified object.
    • Unlock (unlock) Action: The thread releases the lock for the specified object.

Figure 1 Java Legacy memory model

The key here is that because the read operation is performed by the main memory, and the corresponding load is executed by the thread, the read operation and the load operation are loosely coupled. In other words, the transfer of variables between the main memory and the thread working memory is loosely coupled. Similarly, because the store operation is performed by the thread, and the corresponding write is performed by the main memory, the store operation and the write operation are loosely coupled. In other words, the variable pass between the thread's working memory and the main memory is loosely coupled. The old Java memory model defines very strict rules for how Java implementations execute variable read/write, locking/unlock, and read/write of volatile variables. These rules are very complex, please refer to the JVM specification for details, and we will not repeat them here. The old Java memory model guarantees the correctness of multi-threaded routines by ensuring that shared variables are reliably passed between threads of a multithreaded thread through these complex rules.

the new Java Memory Model

starting with JDK5, Java uses the new memory model, and the new memory model completely discards the concept of the old memory model's main memory and working memory, and discards the 8 memory operations of the old memory model. In other words, the new memory model is completely redesigned .

The new memory model introduces a new concept called Happens-before. The concept of Happens-before was originally proposed by Leslielamport in one of his far-reaching papers ("Time,clocksandtheorderingofeventsinadistributedsystem"). Leslielamport uses Happens-before to define a partial-order relationship (partialordering) between events in a distributed system. Leslielamport in this paper, we give a distributed algorithm, which can extend the partial order relation to some kind of whole order relation.

JSR-133 uses the concept of Happens-before to specify two operations (the operation here refers to the sequence of execution between the read/write of the variable in the program, and the lock and unlock of locks). The new memory model defines the following Happens-before rules :

    • Program Order rules: Each action in a thread is happensbefore to any subsequent action in that thread.
    • Monitor lock rule: the unlocking of a lock is happens-before to the subsequent locking of the lock.
    • Volatile variable rule: writes to a volatile field, happens-before to any subsequent reading of this volatile field.
    • Transitivity: If a happens-before B, and B happens-before C, then a Happens-before c.
    • Start () rule: If thread A performs an operation Threadb.start () (boot thread B), then the Threadb.start () operation of the A thread happens-before any action in thread B.
    • Join () rule: If thread A performs an operation Threadb.join () and returns successfully, any action in thread B happens-before the successful return of thread A from the Threadb.join () operation.

Since two operations can be within one thread or between different threads. Therefore, JMM can provide the programmer with a happens-before relationship with a cross-thread memory visibility guarantee (if there is a happens-before relationship between the write operation A and the B thread's read operation B of the A thread, although the a operation and the B operation are performed in different threads, the JMM

Figure 2 Design of a new memory model

Assure the programmer that a operation will be visible to the B operation. in the new memory model to provide programmers with happens-before rules, programmers only need to deal with Happens-before, so the Java Programmer's learning burden is greatly reduced . At the same time, the new memory model allows the reordering of program results to be minimized, which minimizes the binding of compilers and processors, and the new memory model performs better than the old memory model.

Author's profile: Cheng Java software engineer, focusing on concurrency and NIO, author of in-depth understanding of the Java memory model. He is currently working in Nanjing Fujitsu Software Technology Co., Ltd., and is engaged in the development of Fujitsu Interstage Application Server.

The historical changes of the 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.