Java multi-thread interpretation II (memory), java multi-thread interpretation of memory

Source: Internet
Author: User

Java multi-thread interpretation II (memory), java multi-thread interpretation of memory

Thread Memory Structure

I. Primary memory and working memory

1. The main objective of the Java memory model is to define access rules for various variables in the program. The variables here are different from the variables mentioned in Java programming. They include instance fields, static fields, and elements that constitute an array object, but do not include local variables and method parameters, because they are private to the thread, they will not be shared.

2. In the Java memory model, all variables are stored in the primary memory, and each thread has its own virtual memory. The virtual memory of the thread saves the variables used by the thread to the master memory copy. All operations (read and value assignment) on variables by the thread must be performed in its own virtual memory, rather than directly reading and writing the variables in the main memory. Different threads cannot directly access the variables in the virtual memory of the other party. The transmission of Inter-thread variable values must be completed in the main memory.

2. Memory Interaction

The specific interaction protocol between the main memory and the working memory, that is, how to copy a variable from the main memory to the working memory, and how to synchronize it from the working memory to the main memory, the Java memory model defines the following eight operations to complete:

  • Lock: The variable acting on the main memory, marking a variable as a thread exclusive state.
  • Unlock: acts on the main memory variable, releases a variable in the locked state, and the released variable can be locked by other threads.
  • Read: transmits a variable value from the primary memory to the worker memory of the thread for subsequent load operations.
  • Load: The variable acting on the working memory. It puts the variable value obtained from the read operation in the main memory into the variable copy in the working memory.
  • Use: it acts on the variables in the working memory and passes a variable value in the working memory to the execution engine, this operation is performed whenever the VM encounters a bytecode command that requires variable values.
  • Assign: a variable acting on the working memory. It assigns a value received from the execution engine to the variable in the working memory, this operation is executed every time a VM encounters a bytecode command that assigns values to a variable.
  • Store (storage): it acts on the variables in the working memory and transfers the value of a variable in the working memory to the main memory for subsequent write operations.
  • Write: The variable acting on the main memory. It transfers the store operation from the value of a variable in the working memory to the variable in the main memory.

If you want to copy a variable from the main memory to the working memory, you need to perform the read and load operations in sequence. If you want to synchronize the variable from the working memory to the main memory, store and write operations are performed sequentially.

3. Three features of Multithreading

1,Atomicity)

Atomicity means that in an operation, the cpu cannot be paused and then scheduled in the middle, neither interrupted nor completed.It is consistent with the atomicity of things.

2,Visibility)

Visibility means that when a thread modifies the value of the thread shared variable, other threads can immediately learn the modification.From the interaction between multi-thread memory, we know that all the shared data is copied from the primary memory and operated in the virtual memory. Finally, writing data to the primary memory will cause thread insecurity.

If you refresh the variable value from the primary memory before reading the variable, you can use this method to rely on the primary memory as the transmission medium for visibility.

3,Ordering)

Thread orderliness refers to two aspects: if we observe in this thread, all operations are ordered; if we observe in another thread, all operations are disordered. The first half refers to "Serial semantics in the thread", and the last half refers to "Command Re-sorting" and "Master memory synchronization delay in the working memory.

Iv. Synchronization Mechanism

Introduction to volatile, synchronized, and final

 

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.