Java lock (1) Memory Model and java lock Memory Model

Source: Internet
Author: User

Java lock (1) Memory Model and java lock Memory Model

To understand the Java lock mechanism, thread security issues, and data consistency problems, it is necessary to understand the memory model and the Mechanism mechanism, and these problems will be solved.

I. Primary memory and working memory

The Java Memory Model consists of the primary memory and the working memory. All variables are stored in the primary memory. Each thread also has its own working memory. The thread's working memory stores copies of the primary memory used by the thread to variables, all operations on variables by the thread must be performed in the working memory, rather than directly reading and writing the variables in the main memory. Different threads cannot directly access the variables in the working memory of the other party. The transmission of Inter-thread variable values must be completed by the main memory.

Ii. threads, working memory, and main memory

The following is the memory model of java (see and java Virtual Machine)

 

In fact, the processor architecture between different platforms directly affects the memory model structure.

In C or C ++, you can use the memory model of different operating platforms to write concurrent programs. however, what this brings to developers is higher learning costs. in contrast, java uses the advantages of its own virtual machine, so that the memory model is not bound to a specific processor architecture, and truly implements cross-platform. (For different JVMs such as hotspot jvm and jrockit, the memory model will also be different)

Features of the Memory Model:

A. Visibility (multi-core, multi-thread data sharing)

B. Ordering orderliness (operations on memory should be sequential)

In jmm, when modifying the variable value through a concurrent thread, the thread variables must be synchronized back to the primary memory before other threads can access the variable. This reflects the Visibility ). The synchronization mechanism provided by java or the volatile keyword ensures the access sequence of the memory.

3. Memory Interaction

Java memory operations include:

① Lock

A variable acting on the main memory is identified as a State exclusively occupied by a thread;

② Unlock

The variable acting on the main memory, releasing a variable in the locked state;

③ Read

The variable acting on the main memory transmits the value of a variable from the main memory to the working memory of the thread for subsequent load operations;

④ Load

Act on the variables in the working memory, and put the read operation variable value obtained from the main memory into the copy of the variables in the working memory;

⑤ Use

Act on the variables in the working memory and pass the variables in the working memory to the execution engine;

⑥ Assign

Act on the variables in the working memory, and assign the variables received by the execution engine to the variables in the working memory;

7. Store

Act on the variables in the working memory and send the variables in the working memory to the main memory;

Writable Write

Act on the variables in the primary memory, and put the value of the variable operated by the store into the variables in the primary memory.

The following principles must also be met:

1) Read, load, and store and write cannot appear separately;

2) Assign cannot be discarded;

3) variables without the assign operation cannot be synchronized from the thread's working memory to the main memory;

4) assign and load are required before Use and store operations;

5) the Lock and unlock appear in pairs to unlock the object;

6) objects without lock cannot be unlocked;

7) When the Lock operation is executed, the value of this variable in the working memory is cleared;

8) before unlock is executed, the object must be synchronized to the main memory.

The above discussion seems a bit confused at the beginning. You can think over the model diagram to see it at a glance.

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.