From five aspects so that you really understand the Java Memory Model

Foreword

First we look at until we know what java memory model computer memory model, memory model to understand the computer, then later will be much simpler looking JMM.

v2-7db71092c88d35b2fc5d3e0c67cac5a2_hd.png

Computer Memory

Computer, the main memory by the CPU, disk, etc. (simple extraction boil problem) we know that all instructions of a computer program executed by the CPU are performed, when the execution data is to be processed, the data in main memory typically stores in.

v2-e671563696762e2fcf892908742e7e4d_hd.jpg

As shown, this time the problem came faster and faster execution speed of the CPU, and memory are fine progress, so read and write operations of the CPU will be very time-consuming, inefficient on very low?

So this time there have been Cache (Cache) to solve this problem, then what is it cache? Cache is actually saved data kept, characterized by fast. So this program execution time becomes like this: First, put a data assignment in the cache from main memory at run time, and then go directly to the CPU cache read and write data at the time of operation, etc. after performing the refresh data to the main memory. Such an action would greatly improve the speed of execution. We look at the flow chart:

v2-8dc967ac63d5650f4f9e8ae8329f5471_hd.gif

As it can be seen, when the first operation of the L1 cache data read out from the main memory, and CPU data read operation from the cache, when the data is finished, flushed from the cache to the main memory. With the ability to execute more powerful the CPU, the buffer layer has been unable to meet demand, and this time there have been Level 2 cache (L2Cache) Level 3 cache (L3Cache), each level cache is stored is the next level cache part of the data.

In this way it will perform when the CPU needs data when: First, go to a cache (L1Cache) to find, if not go to a cache secondary cache (L2Cache) find, there is no secondary cache lookup went three-level cache (L3Cache) If the cache is not, go to the main memory lookup. So the question came.

Cache coherency

Modern computers have not a single CPU, a plurality of the CPU, the CPU of each may be a multi-core, only a single-core CPU cache is said above, respectively L1, L2, L3 as shown:

v2-5ad975acf530ee1da4ec62d61f6d676d_hd.jpg

If a plurality of CPU core, then, it is that each has a core with a L1 cache or the L2 cache, and the L2 cache or a shared L3 cache.

We look at the structure:

v2-77835e3127e689bc815d714337c4e24e_hd.jpg

这个时候每个核心都有自己的高速缓存,它们又共享同一主存,就会造成缓存一致性的问题,在多线程同时访问同一共享数据的情况下,每个线程都是操作自己缓存的数据副本,这个时候就会出现每个缓存中的共享数据存在不一致的情况。多个处理器运算任务都涉及同一块主存,需要一种协议可以保障数据的一致性,这类协议有MSI、MESI、MOSI及Dragon Protocol等。

处理器优化

上面了解到提高CPU的效率就是在CPU和主存直接增加高速缓存,增加高速缓存会造成缓存不一致的问题,除了缓存不一致的问题,还有一种问题就是为了能让处理器内部的运算单元能够尽量的被充分利用处理器可能会对输入代码进行乱序执行,并且处理器会在计算之后将乱序的代码进行结果重组来保证结果的一致性。在Java虚拟机中也有类似的指令重排序

思考

这篇文章其实是讲述java内存模型的,为什么会和计算机硬件扯上关系呢?注意到上面有说到多线程的情况下会造成缓存不一致的问题,提到多线程就离不开并发,想到并发的话就离不开三大问题,可见性,原子性,有序性的问题。那这三种特性不就是上面所说到的缓存不一致,处理器优化和指令重排序问题吗。这这样看来缓存不一致不就是可见性的问题,而原子性不就是处理器优化所导致的原子性问题,指令重排序就是导致有序性的问题。那么Java内存模型又是什么呢?

Java内存模型

Java内存模型的作用就是用来屏蔽掉不同操作系统中的内存差异性来保持并发的一致性。同时JMM也规范了JVM如何与计算机内存进行交互。简单的来说java内存模型就是Java自己的一套协议来屏蔽掉各种硬件和操作系统的内存访问差异,实现平台一致性达到最终的"一次编写,到处运行"。看到这里就知道了Jmm是用来做什么的。同时Java内存模型可以理解为java并发内存模型。然后JMM

通信

Java memory model (hereinafter referred to as JMM) states that all of the variables are stored in the main memory, each thread has its own local cache, so the operation thread of the variables must be not directly operate the main memory in the local cache , other threads can not access variables between threads, if you want to communicate can only communicate through the main memory.

JMM abstract diagram:

v2-c50f5fbbf62a2a0cb36be28f81648fe6_hd.jpg

From the graph we can see that each thread has a local memory, if the thread if you want to communicate about the steps to be performed:

  • A first thread-local value written in the main memory of the memory

  • Thread A Thread B taken read from main memory to write the value of

Here we have a clear understanding of the JMM. JMM actually a norm, its main purpose is to solve the multi-thread local memory is generated when data communication by shared memory is inconsistent, the compiler will reorder code instructions, the processor would be out of order execution code is brought problem. Welcome to the concern I kind of public-ho [programmers] marks the beginning of the article will be updated on the inside, documentation will be on the inside.

solved problem

JMM issues solved inseparable from what we call the above three characteristics: visibility, atomicity, ordering.

Atomicity: using the keyword to ensure synchronized atoms in java code.

Visibility: volatile keyword to ensure the visibility of multi-threaded manipulated variables, while synchronized final and can guarantee the visibility of variables, note: volatile does not guarantee atomicity, so when a volatile must pay attention to.

Ordering: volatile rearrangement can disable command, the synchronized keyword to ensure the same time allowing only one thread synchronized operations so we can find three kinds of problems can be solved, so use more synchronized keyword, but allows only one thread synchronized operation, efficiency will cause a context switch.

At last

Welcome to share with everyone, like the point of a praise yo remember the article, thanks for the support!


Guess you like

Origin blog.51cto.com/14442094/2442113