2015 27th Wednesday Java memory model

Source: Internet
Author: User

Write their own code, 6 months do not look at the code of others, their knowledge is also the same, learned knowledge if not used or not often reviewed, then not their own knowledge.

To understand Java thread safety, you must know two main points: Java's memory model, Java's thread synchronization mechanism. In particular, the memory model, Java's thread synchronization mechanism is largely based on the memory model and set.

talking about Java memory model
Different platforms, the memory model is not the same, but the JVM's memory model specification is unified. In fact, Java's multi-threaded concurrency problem will eventually be reflected in the Java memory model, so-called thread safety is nothing more than to control the multiple threads of a resource ordered access or modification. Summarize the Java memory model to solve two major problems: visibility and ordering. We all know that the computer has a cache, and the processor does not process the data every time it takes memory. The JVM defines its own memory model, shielding the underlying platform memory management details, and for Java developers, be clear on the JVM memory model, if the visibility and ordering of multithreading is addressed.
So what is visibility? Communication between multiple threads cannot be communicated to each other, and they can only be communicated through shared variables. The Java Memory model (JMM) specifies that the JVM has primary memory and that the main memory is shared by multiple threads. When the new object is also allocated in the main memory, each thread has its own working memory, the working memory stores a copy of some objects of memory, of course, the size of the working memory of the thread is limited. When a thread is manipulating an object, the order of execution is as follows:
(1) Copying variables from main memory to current working RAM (read and load)
(2) Execute code, change shared variable value (use and assign)
(3) Refresh main memory related content (store and write)

The JVM specification defines the operation instructions for the thread to main memory: Read,load,use,assign,store,write. When a shared variable has a copy in the working memory of multiple threads, if a thread modifies the shared variable, the other thread should be able to see the modified value, which is the problem of multi-threaded visibility.
So, what is Order? A thread cannot reference a variable directly from the main memory, and if the thread does not have the variable in its working memory, it copies a copy from the main memory to the working memory, which is read-load, and the thread references the copy when it is finished. When the same thread references the field again, it is possible to retrieve a copy of the variable from main memory (Read-load-use), or it may refer directly to the original copy (use), meaning that the read,load,use order can be determined by the JVM implementation system.

2015 27th Wednesday 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.