Concurrent programming and high concurrency solution learning (Java memory model)

JMM(Java Memory Model)

    JMM is a specification that specifies how the Java virtual machine and computer memory work together, specifying how and when a thread can see the values ​​of shared variables modified by other threads, and if necessary, if synchronized access Shared variables.




stack
    The advantage of the stack: the access speed is faster than the heap, second only to the registers in the computer, and the data of the stack can be shared. Disadvantages: The size of the stored data and the generator must be determined, and the stack mainly stores the handles of basic type variables and objects. The Java memory model requires that call stacks and local variables are stored on the thread stack, and objects are stored on the heap.

heap

       The heap in Java is the runtime data area. The heap is responsible for garbage collection. The heap can dynamically allocate memory size. The claim period does not have to tell the compiler, because it allocates memory dynamically at runtime. Java's garbage collector will Automatically reclaim these unused data, disadvantage: slow access speed

                                                        Hardware Memory Architecture Diagram


 The CPU can perform operations on registers much faster than in main memory because the CPU can access registers much faster than main memory.

Cache: Due to the difference of several orders of magnitude between the computer's storage device and the processor's operating speed, now computer systems have to add a layer of cache with a read and write speed as close to the processor's speed as possible for memory and processing. The buffer between the processors copies the data used in the operation into the cache, so that the operation can be performed quickly, and after the operation is completed, it is synchronized back to the memory from the cache, so that the processor does not need to wait for the slow memory read and write, and the CPU accesses the cache layer is faster than accessing main memory, and usually a little slower than accessing internal registers. Each CPU has a CPU cache layer, and a CPU also has multiple layers of cache, and at a certain moment, one or more cache lines may be read from the cache and one or more cache lines may be flushed to main memory.

Memory: A computer also contains a main memory, which is accessible to all CPUs. Main memory is usually much larger than the CPU's cache.

                                                        Java memory model


The hardware memory architecture does not distinguish between threads, stacks, and heaps. For hardware, all thread stacks and heaps are distributed in the main memory, and some thread stacks and heaps may appear in the CPU cache and registers inside the CPU.

                                                             Java memory model abstract structure diagram


Java Memory Model - Synchronizing Eight Operations
lock (lock): a variable acting on the main memory, marking a variable as the exclusive state of a line city
unlock (unlock): a variable acting on the main memory to release a variable in a locked state, and the released variable can be locked by other threads
read (read): A variable that acts on memory and transfers a variable value from main memory to the thread's working memory for subsequent load actions.
load (load): a variable acting on working memory, which puts the variable value obtained from the main memory by the read operation into the variable copy of the working memory.
use (use): a variable acting on working memory, passing a variable value in working memory to the execution engine
assign (assignment): a variable acting on working memory, which assigns a value received from the execution engine to a variable in working memory
store (storage): a variable acting on working memory, transferring the value of a variable in working memory to main memory for subsequent write operations
write (write): a variable acting on main memory, which transfers the store operation from the value of a variable in working memory to a variable in main memory

Java Memory Model - Synchronization Rules

※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 synchronize the variable from the working memory back to the main memory, you need to perform the store and write operations in sequence. But the Java memory model only requires that the above operations must be performed sequentially, and there is no guarantee that they must be performed consecutively.

※One of read and load, store and write operations is not allowed to appear alone

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

※A thread is not allowed to synchronize from working memory back to main memory for no reason (no assign operation has occurred)

※一个新的变量只能在主内存中诞生,不允许在工作内存中直接使用一个未被初始化(load或assign)的变量。即就是对一个变量实施use和store操作之前,必须先执行assign和load操作。

※一个变量在同一时刻只允许一个线程对其进行lock操作,单lock操作可以被同一条线程重复执行多次,多次执行lock后,只有执行相同次数的unlock操作,变量才会被解锁。lock和unlock必须成对出现。

※如果一个变量执行lock操作,将会请求工作内存中此变量的值,在执行引擎使用这个变量前需要重新执行load和asign操作初始化变量的值。

※如果一个变量事先没有被lock操作锁定,则不允许对它执行unlock操作,也不允许去unlock一个被其他线程锁定的变量

※对一个变量执行unlock操作之前,必须先把此变量同步到主内存中(执行store和write操作)



更多关于Java内存模型中堆的讲解请查看http://www.importnew.com/14630.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325324361&siteId=291194637