Java Concurrency Knowledge Collection (Pre-knowledge--java memory model)

Source: Internet
Author: User

JMM Specification:

I. Concept: Regulates how Java Virtual machines and computer memory work together, and it specifies how and when a thread can see the values of shared variables modified by other threads, and how to access shared variables synchronously when necessary.

Note: If two threads invoke the same method of the same object at the same time, they will access the object's member variables, at which point the two threads have only a private copy of the object.

   

    

Two. Eight kinds of synchronous operation

1. Luck (lock): A variable that acts on the main memory, which marks a variable as a thread-exclusive state.

2. Unlock (unlock): A variable that acts on the main memory, releasing a variable that is in a locked state, and the released variable can be locked by another thread.

3. READ: A variable acting on the main memory, which transfers the value of a variable from main memory to working memory for subsequent load actions.

4. Load (load): A variable that acts on the working memory, which puts the value of the variable that the read operation obtains from main memory into a variable copy of the working memory.

5. Use: A variable acting on a working memory that passes the value of a variable in the working memory to the execution engine, which is performed whenever the virtual opportunity is a worthwhile bytecode instruction that needs to be used to a variable.

6, assign (Assignment): A variable acting on the working memory, which assigns a value received from the execution engine to the working memory variable, which is performed whenever the virtual opportunity is assigned to a byte-code instruction that assigns a variable value.

7. Store: A variable acting on a working memory that passes the value of a variable in the working memory to the main memory for subsequent write operations to use.

8. Write (write): A variable that acts on the main memory, which puts the value of the store operation from the working memory into the variable of the main memory.

Three. Corresponding synchronization rules

1. Read and load, store, and write are not allowed to appear separately. That is, a variable is not allowed to read from the main memory but the working memory is not acceptable, or from within the work from the initiation will be written but the main memory is not acceptable.

2. A thread is not allowed to discard its most recent assign operation, that is, the variable must be synchronized to main memory after it has changed in working memory.

3. One thread is not allowed to synchronize data from the thread's working memory back to main memory without any reason, that is, no assign operation has occurred and is synchronized to main memory.

4, a new variable can only be born in the main memory, not allowed in the working memory directly using a non-initialized (load or assign) variable, in other words, the use of a variable, store operation, you must first perform the Assign and load operations.

5, a variable at the same time, only one thread is allowed to loack operation, but the lock operation can be repeated multiple times by the same thread, after multiple execution of lock, only perform the same number of unloack operations, the variable can be unlocked.

6. If a lock operation is performed on a variable, it will empty the value of this variable in the working memory, and the load or assign operation initialization variable value needs to be re-executed before the execution engine uses this variable.

7, if a variable is not locked by lock operation in advance, it is not allowed to perform unlock operation on him, nor allow to unlock a variable locked by another thread

8. Before performing a unlock operation on a variable, you must first synchronize this variable back into main memory (execute store, write operation).

Four. The operation and rule diagram is as follows:

   

Note: Thank you jimin teacher, do this concurrent set of knowledge summary of the original intention is due to read his "Java Concurrency programming and high concurrency solution" generated ideas.

Java Concurrency Knowledge Collection (Pre-knowledge--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.