Java thread working memory and main memory variable exchange process and volatile keyword understanding

Source: Internet
Author: User
Tags volatile

Java thread working memory and main memory variable exchange process and volatile keyword understanding

1. The Java memory model specifies that in multi-threaded situations, threads manipulate main memory variables and require copies of the main memory variable to be copied by the thread's unique working memory. The so-called memory model here differs from what is commonly referred to as the virtual machine heap model:

2. The thread-specific working memory and process memory (main memory) are implemented through 8 atomic operations, as shown in:

Rules for atomic operations (partial):

1) The read,load must be executed continuously, but the atomicity is not guaranteed.

2) The Store,write must be executed continuously, but the atomicity is not guaranteed.

3) You cannot lose a copy of the last assign operation of the variable, that is, the copy that iterates through the last assign must be written back to Mainmemory.

Other rules see "in-depth understanding of Java Virtual Machines" chapter 12th Java memory Models and threading

3. Volatile keyword

A variable defined as a volatile type has two semantics:

1) Changes to variables are visible to all threads

Understand:

  Each use variable in a thread requires successive read->load->use operations, that is, each usage updates the variable value from the main memory so that the other thread's modifications are visible to the thread.

Each time a thread assign a variable, it needs to perform several assign->store->write operations sequentially, that is, it writes back to main memory after each update so that all other threads are reading the latest data.

2) Prohibit command rearrangement

Reference:

"In-depth understanding of Java Virtual Machines" Chapter 12th Java memory model and threading

Java thread working memory and main memory variable exchange process and volatile keyword understanding

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.