Java concurrent Programming--7.java memory Operations Summary

Source: Internet
Author: User

Main memory and working memoryworking Rules

Java memory model, defining access rules for variables, storing shared variables in memory and removing the underlying details of memory

All variables are stored in main memory, each thread has its own working memory, the variables used in working memory are copies from the main memory copy, all the operations of the thread-to-variable are in working memory, and the inter-thread variables are worth passing through the main memory to complete
Inter-memory interaction operations
1, Luck (locked): A variable that acts on the main memory, which marks a variable as a thread-exclusive state. 2, Unlock (unlocked): A variable that acts on the main memory, frees a variable that is locked, and the released variable can be locked by another thread. 3, read: A variable that acts on the main memory, which transfers the value of a variable from main memory to working memory for subsequent load actions to use. 4, Load (load): A variable that acts on the working memory, which puts the value of the variable that the read operation obtains from main memory into a variable copy of the working memory. 5, use: A variable acting on a working memory that passes the value of a variable in the working memory to the execution engine, which is performed whenever the virtual opportunity is a worthwhile bytecode instruction that needs to be used to a variable. 6, assign (Assignment): A variable acting on the working memory that assigns to the working memory a value received from the execution engine, which is performed whenever the virtual opportunity is assigned to a byte-code instruction that assigns a value to the variable. 7, Store: A variable acting on a working memory that passes the value of a variable in the working memory to main memory for subsequent write operations to use. 8, write: A variable that acts on the main memory, which puts the value of the store operation from the working memory into the variable of the main memory.

To do this, the following rules must be met:

 1, does not allow one of the read and load, store, and write operations to appear separately, the above two operations must be executed sequentially, but there is no guarantee that continuous execution is required, that is, between read and load, and between store and write, other instructions can be inserted. 2, a thread is not allowed to discard its most recent assign operation, that is, the variable must be synchronized back to main memory after it has changed in working memory. 3, a thread is not allowed to synchronize data from the working memory of the thread back to main memory for no reason (no assign operation has occurred). 4, a new variable can only be "born" from main memory, does not allow direct use of a variable that is not initialized (load or assign) in working memory, in other words, the Assign and load operations must be performed before a variable is implemented with a use and store operation. 5, a variable allows only one thread to perform a lock operation on it at the same time, but the lock operation can be repeated multiple times with the same thread, and once the lock is executed more than once, the variable is unlocked only by performing the same number of unlock operations. 6, if you perform a lock operation on a variable, the value of this variable in the working memory will be emptied, and the value of the initialization variable of the load or assign operation needs to be re-executed before the execution engine uses the variable. 7, if a variable implementation is not locked by the lock operation, it is not allowed to perform a unlock operation on it, nor is it allowed to unlock a variable that is locked by another thread. 8, you must first synchronize this variable back to main memory (perform store and write operations) before performing a unlock operation on a variable.
Happen-before

Read and write operation of shared data, if the Happen-bifore condition is not met, there is no order guarantee, the virtual machine can reorder them

Re-order

In other words, as long as the final result of the program is equal to its result in a strictly sequenced environment, then the order of execution of the instruction may be inconsistent with the order of the code, a process known as a reordering of instructions.

The meaning of command reordering is that the JVM is able to properly reorder machine instructions based on the characteristics of the processor (multi-level cache system, multi-core processor, etc.), so that the machine instruction is more consistent with the performance of the CPU and maximizes the machine

Special Memory operations

Volatile modified variables: visibility; Disable command reordering optimization


Final field: The final field value can be seen in the other thread once it is initialized in the constructor


Special rules for long and double variables: Except for long and double types, the other base types and the assignment of reference types are atomic operations

Java concurrent Programming--7.java memory Operations Summary

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.