JAVAJMM model details

This is the 11th day of my participation in the August More Text Challenge

What is the JMM model?

The Java Memory Model (JMM) is an abstract concept that doesn’t really exist. It describes a set of rules or specifications that define how variables in a program (including instance fields, static fields, and elements that make up array objects) can be accessed. The JVM’s running entity is a thread, and each time a thread is created, the JVM creates a working memory (called stack space in some places) for it to store thread-private data!

Specified in the Java memory model all variables are stored in main memory, main memory is Shared memory region, all threads can access, but the thread to the operation of the variable (reading assignment, etc.) must be conducted in the working memory, first of all variables from the main memory to copy their working memory space, and then to operating variables, Operation to complete before you write variables back to main memory, no direct operating variables in main memory, working memory is stored in main memory the variables in the copy of a copy of the said earlier, the working memory is the private data area each thread, so don’t have access to the other side of the work between different threads of memory, the communication between threads (by value) must be done through the main memory!

The JMM is different from the JVM memory region model

The JMM and JVM memory partitions are at a different conceptual level. More specifically, THE JMM describes a set of rules that govern how variables in a program can be accessed in shared and private data areas. The JMM focuses on atomicity, order, and visibility. JMM and Java memory area only similarities, there are Shared data area and the private data area, main memory in JMM belongs to the Shared data area, from a certain extent area should include the heap and method, and thread the private data area, the working memory data from a certain extent it should be including the program counter, virtual machine as well as the method of local stack

Thread, working memory, Main memory working interaction diagram (based on JMM specification):

Main memory

The main store is Java instance objects. All thread-created instance objects are stored in main memory, whether they are member variables or local variables (also known as local variables) in methods, as well as shared class information, constants, and static variables. Since the data area is shared, multiple threads accessing the same variable can cause thread-safety issues!

The working memory

Each thread can only access its own working memory. That is, local variables in a thread are not visible to other threads, even if two threads execute the same code. They also create local variables in their working memory that belong to the current thread, including bytecode line number indicators and information about Native methods. Note that since working memory is the private data of each thread, threads cannot access working memory with each other, so data stored in working memory is not thread-safe

The relationship between JMM and hardware memory architecture

From the previous hardware memory architecture, The Java memory model, and the implementation of Java multithreading, we should realize that the execution of multithreading eventually maps to the hardware processor, but the Java memory model and the hardware memory architecture are not exactly the same. Only for hardware memory registers, the concept of cache memory, primary memory, not the working memory (thread the private data area) and main memory (heap memory), that means the division of the memory of the Java memory model for hardware memory and does not have any effect, because the JMM is an abstract concept, is a set of rules that does not actually exist, Whether working memory data or data from main memory, for computer hardware will be stored in the computer main memory, of course, may also be stored in the CPU caches or register, so in general, the Java memory model and the computer hardware memory architecture is an overlapping relations, is an abstract concept division and cross of the real physical hardware. (Note that the same is true for Java memory partitioning.)

Why JMM

After understanding the Java memory region division, hardware memory architecture, the implementation principle of Java multithreading and the specific relationship between the Java memory model, then talk about the necessity of the Existence of the Java memory model. Because the JVM to run the program of the entity is a thread, and every thread creation when the JVM to create a working memory (called the stack space) in some places, and threads used to store the private data, threads and the main memory, the variables in the operation should be done indirectly by working memory, is the main process variables from the main memory each thread its own copy of the working memory space, After the operation is complete, the variable is written back to main memory. If two threads operate on the variable of an instance object in main memory at the same time, thread-safety problems may occur.

Suppose there is A shared variable x in main memory. Now there are two threads, A and B, respectively operating on the variable x=1. A/B has A copy of the shared variable X in their respective working memory. Suppose thread A now wants to change the value of x to 2, and thread B wants to read the value of x. Does thread B read the value of 2 after thread A updated it or 1 before it updated it?

The answer is, not sure!

Namely B thread may read A thread to update the value of 1, before the may also read A thread on the updated value 2, this is because the working memory is each thread of the private data area, and thread A variable x, the first is to variable copy from main memory to A thread’s working memory, and then to manipulate, the variable operation is completed before the variable x to write back to the Lord!

For B threads are similar, so it may cause data consistency problems between main memory and working memory, if A thread after the modification is to write data back to the main memory and the thread B is read from main memory at this time, the x = 1 copy to their working memory, this thread B read value is x = 1, But if thread A writes x=2 back to main memory before thread B starts reading, then thread B will read x=2, but which comes first?

As shown in the following example diagram:

The Java memory model defines the following eight operations to implement the protocol for interaction between main memory and working memory, i.e. how a variable is copied from main memory to working memory and synchronized from working memory to main memory:

  • Lock: A variable that acts on main memory, marking a variable as a thread-exclusive state

  • Unlock: A variable that operates on main memory. It releases a locked variable so that it can be locked by another thread

  • Read: a variable acting on main memory that transfers a variable value from main memory to the thread’s working memory for subsequent load action

  • Load: Variable acting on working memory, which puts the value of the variable obtained from main memory by the read operation into a copy of the variable in working memory

  • Use: variable applied to working memory, passing the value of a variable in working memory to the execution engine

  • Assign: A working memory variable that assigns a value received from the execution engine to the working memory variable

  • Store: Variable applied to working memory that transfers the value of a variable in working memory to main memory

For subsequent write operations

  • Write: variable acting on working memory that transfers store operations from the value of a variable in working memory to a variable in main memory

Note: The Java memory model only requires that the above operations be performed sequentially, not consecutively!

Synchronization Rule Analysis

The JMM has the following synchronization rules:

  • A thread is not allowed to synchronize data from working memory back to main memory without a cause (without any assign operation)

  • A new variable can only be created in main memory. It is not allowed to use an uninitialized (load or assign) variable in working memory. The use and store operations on a variable must be assigned and loaded.

  • A variable can be locked by only one thread at a time. However, the lock operation can be repeated by the same thread several times. After the lock operation is performed several times, the variable can be unlocked only after the same number of UNLOCK operations are performed. Lock and unlock must come in pairs.

  • If a lock operation is performed on a variable, the value of the variable will be emptied from working memory, and the load or assign operation will be re-performed to initialize the variable before the execution engine can use it.

  • Unlock cannot be performed on a variable that has not been previously locked by a lock operation. It is also not allowed to unlock a variable that has been locked by another thread.

  • Before you can unlock a variable, you must first synchronize it to main memory (store and write)

summary

Hopefully this has helped you learn more about models other than JVM models. We will cover atomicity, visibility, and order in volatile!

Thanks for reading!