Java Multithreading Interpretation II (Memory)

Source: Internet
Author: User

Memory structure diagram of the thread

primary Memory vs. working memory

The primary goal of the 1.JAV a memory model is to define access rules for each variable in the program. The variables here are different from those described in Java programming, which include instance fields, static fields, and elements that make up an array object, but do not include local variables and method parameters because they are thread-private and are not shared.

The 2.Java memory model specifies that all variables are stored in main memory, and each thread has its own virtual memory. The thread's virtual memory holds the variable used by the thread to the master memory copy. all operations of a thread on a variable (read, assign) must be made in its own virtual memory, and cannot be read directly to the variables in main memory. Variables in each other's virtual memory cannot be accessed directly between different threads, and the transfer of variable values between threads needs to be done in main memory.

Second, inter-memory interaction operation

The Java memory model defines the following eight actions for the specific interaction protocol between main memory and working memory, that is, how a variable is copied from main memory to working memory, how to synchronize from working memory to main memory.

    • Lock: A variable that acts on the main memory and identifies a variable as a thread-exclusive state.
    • Unlock (Unlocked): Acts on the main memory variable, releasing a variable that is in a locked state, and the released variable can be locked by another thread.
    • READ: Acts on the main memory variable, transferring a variable value from main memory to the working memory of the thread for subsequent load actions to use
    • Load: A variable that acts on working memory, which places the value of a read operation from the main memory into a variable copy of the working memory.
    • Use: A variable that acts on the working memory, passing a variable value in the working memory to the execution engine, which is performed whenever the virtual opportunity is to a bytecode instruction that needs to use the value of the variable.
    • Assign (Assignment): A variable acting on a working memory that assigns a value to a working memory from the execution engine, and performs this operation whenever the virtual opportunity is assigned to a byte-code instruction that assigns a value to a variable.
    • Store: A variable acting on a working memory that transfers the value of a variable in the working memory to the main memory for subsequent write operations.
    • Write: A variable that acts on the main memory, which transfers the store operation from the value of a variable in the working memory to a variable in the main memory.

If you want to copy a variable from main memory to working memory, you will need to follow the read and load operations, and if you synchronize the variables from the working memory back to main memory, you should perform the store and write operations sequentially.

Three, multi-threaded three features

1. atomicity (atomicity)

atomicity means that in one operation the CPU is not allowed to pause and then dispatch, neither interrupted nor executed. is consistent with the atomicity of things.

2. Visibility (Visibility)

visibility means that when a thread modifies the value of a thread-shared variable, other threads can immediately know the change. interaction from multithreaded memory we know that the shared data is from the primary memory copy in the virtual memory operation, the last write to the main memory, it will cause the thread is unsafe.

If you refresh the variable value from main memory before the variable is read, you can use this dependent main memory as a means of passing the media to achieve visibility.

3, Order (ordering)

The ordering of threads refers to two aspects: if you observe within this thread, all operations are orderly, and if another thread is observed in one thread, all operations are unordered. The first half of the sentence refers to "line range performance as serial semantics", the latter sentence refers to the "order reordering" phenomenon and "the main memory synchronization delay in working memory" phenomenon.

Iv. synchronization mechanism

Describes volatile, synchronized, final

Java Multithreading Interpretation II (Memory)

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.