JVM-concurrency-Java memory model, jvm-concurrency-java model

Source: Internet
Author: User

JVM-concurrency-Java memory model, jvm-concurrency-java model
Java Memory Model

(1). Master memory and working memory

The Java Memory Model specifies that all variables are stored in the primary memory.

Copies of the primary memory of each type of thread variables. All operations (read operations, value assignment operations, etc.) of the thread on the variables must be performed in the working memory, rather than directly reading and writing the variables in the primary memory.

Different threads cannot directly access the variables in the working memory of the other party. Inter-thread variables must be transmitted through the main memory.

(2) memory-to-memory operations

The main memory interacts with the working memory. The Java Memory Model defines eight operations to complete.

A) lock: The variable acting on the main memory. It identifies a variable as a linear exclusive state.

B) ublock: The variable acting on the main memory. It releases a variable in the lock state and the released variable can be locked by other threads.

C) read: The variable acting on the main memory. It transfers the value of a variable from the main memory to the memory of the thread for subsequent load operations.

D) load: The variable acting on the memory. It puts the variable value obtained from the read operation in the main memory into the variable copy in the working memory.

E) use (use): acts on variables in the working memory. It passes the value of a variable in the working memory to the execution engine, this operation is performed whenever the VM encounters a bytecode command that requires variable values.

F) assign (assign value): applies to variables in the working memory. It assigns the value received by an execution engine to the variables in the working memory, this operation is executed every time the VM encounters a bytecode command that assigns values to the variables.

G) store (storage): The variable acting on the working memory. It transfers the value of a variable in the working memory to the main memory for subsequent write operations.

H) write (write): The variable acting on the main memory. It puts the value of the variable obtained from the store operation in the working memory into the main memory variable.

(3) the Java Memory Model specifies that the following rules must be met when performing the above eight basic operations:

A) read and load operations are not allowed. One of the store and write operations is independent. This means that a variable is not allowed to be read from the primary memory but the back-to-write operation in the working memory is unacceptable.

B) a thread is not allowed to discard its latest assign operation. That is, after the variable changes in the working memory, the variable must be synchronized to the main memory.

C) It is not allowed for a thread to synchronize data from the working memory of the thread without any assign operation.

D) a new variable can only be "generated" in the main memory. It is not allowed to directly use an uninitialized variable in the working memory. In other words, it is used for a variable. Before the store operation, the assign and load operations must be performed.

E) a variable can be locked by only one thread at a time and can be repeatedly executed multiple times by the same thread. After multiple locks are executed, only the same del unlock operation can be executed, the variable is unlocked.

F) If a variable performs the lock operation, it will clear the value of this variable in the working memory. Before the execution engine uses this variable, it needs to re-execute the load or assign operation to initialize the value of the variable.

G) If a variable is not locked by the lock operation in advance, it is not allowed to perform the unlock operation on it, nor to allow unlock a variable that is locked by other threads.

H) before performing the unlock operation on a variable, you must synchronize the variable to cook the main memory (perform the store and write operations)

(4) Special rules for volatile variables:
A) only visibility is guaranteed.

B) disable command re-sorting Optimization

(5) Special rules for long and double variables:

A) Java Memory Model stipulates that virtual machines are allowed to divide the read/write operations of 64-bit data not modified by volatile into two 32-bit operations, and allows virtual machines to achieve selection can not guarantee the atomicity of the 64-bit data type load, store, read and write operations. This is the non-atomic protocol of long and double.

(6) atomicity, visibility, and orderliness

Atomicity: Atomic variable operations that are directly guaranteed by the Java memory model include read, load, assign, use, store, and write.

Visibility: When a thread modifies the value of the shared variable, other threads can immediately learn the modification.

Orderliness: if we observe in this thread, all operations are ordered. If we observe in another thread, all operations are disordered.

(7) Principle of First Occurrence

The relationship between the two operations defined in the Java memory model at the time of first occurrence. If operation A first occurs in operation B, it is actually before operation B, the impact of Operation A can be observed by Operation B. "Impact" includes modifying the value of shared variables in the memory, sending messages, and calling methods.

Program order rules: In a thread, write the first operation in the order of program code before the operation that occurs in the back. To be accurate, it should be the control flow order rather than the program code order, because the branch, loop, and other structures should be considered.

Tube lock rules: an unlock operation first occurs when the lock operation is performed on the same lock.

Volatile variable rules: write operations on a volatile variable first take place before the read operations on the variable. The "back" here refers to the order of time.

Thread startup rules: the start method of the Thread object first occurs in every action of this Thread.

Thread termination rules: all operations in the Thread are checked prior to the termination of the Thread. We can end with the Thread. join () method. THread. Alive () return value and other means to check that the THread has terminated the execution

Thread interruption rules: the call to the Thread interrupt () method first occurs when the code of the interrupted Thread detects the occurrence of the interruption event. You can use the Thread. the interrupted () method checks whether there is any interruption.

Object termination rule: the initialization of an object (the execution of the constructor ends) first occurs at the beginning of its finalize () method.

Passability: If operation A first occurs in operation B and operation B first occurs in operation C, the conclusion that operation A first occurs in operation C is obtained.

Related Article

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.