Concurrent basic knowledge - MESI - JMM

A, CPU multi-level cache

CPU frequency too fast, approaching the main memory can not keep up, so that the processor clock cycle, CPU often needs to wait for main memory, a waste of resources, so there the cache, in order to ease between the CPU and memory speed mismatch problem.
CPU multi-level cache configuration (evolution):

the principle of locality:
(1) temporal locality: If a data is accessed, then it is likely to be accessed again in the near future.
(2) spatial locality: If a data is accessed, then with his adjacent data may also be accessed quickly.


1. cache coherence (MESI the Modify | Exclusive | Share | Invalid)



    the Modify: is modified, only the cache line is cached in the CPU cache. And is being modified, so it is with main memory data is inconsistent, the cache line is written back to memory needs in the future point in time of main memory, this point in time is to allow other CPU reads the main memory before the corresponding memory. When the value here is written back to main memory, the state of the cache line will become Excluisive.
    Exclusive: exclusive, only the cache line is cached in the CPU cache, he is not modified, is the main the consistent data storage. He may at any time be read by the memory of other CPU, become a Share. When the CPU to modify his content into the Modify
    Share: share, means that the cache line may be more than the CPU cache, and the data in each cache and main memory data are consistent. When there is a CPU to modify the cache line when the cache line in the other CPU becomes Invalid
    Invalid: Invalid

        four operating

    local read local read: Read local cache
    local write local write: Write local cache
    distal reading remote rade : the data Memory is read over
    the distal writing remote write: Memory write back the data in the
    cache is modified when the case:     a time the cache is shared CPU a and CPU B, CPU a time to modify local cache when the main memory data will CPU B shared data is invalidated. Cached by S -> the I 2, out of order execution optimization processor improve processing speed has been made to optimize the code violation of the original sequence.
  





For example: The initial demand is calculated as the expected calculation process: (out of order after optimization) the actual calculation process:







 

 

 

 

 

 

 

Two .JAVA memory model (JMM)

一种规范,规范了java虚拟机与计算机内存如何协同工作的。它规定了**一个线程如何和何时可以看到其他线程修改过的共享变量的值,以及在必须时如何同步地访问共享变量。

    堆Heap:运行时数据区,有垃圾回收,堆的优势可以动态分配内存大小,生存期也不必事先告诉编译器,因为他是在运行时动态分配内存。缺点是由于运行时动态分配内存,所以存取速度慢一些。
    栈Stack:优势存取速度快,速度仅次于计算机的寄存器。栈的数据是可以共享的,但是缺点是存在栈中数据的大小与生存期必须是确定的。主要存放基本类型变量,对象据点。要求调用栈和本地变量存放在线程栈上。
    静态类型变量跟随类的定义存放在堆上。存放在堆上的对象可以被所持有对这个对象引用的线程访问。
    如果两个线程同时调用了同一个对象的同一个方法,他们都会访问这个对象的成员变量。但是这两个线程都拥有的是该对象的成员变量(局部变量)的私有拷贝。—[线程封闭中的堆栈封闭]



    CPU Registers(寄存器):是CPU内存的基础,CPU在寄存器上执行操作的速度远大于在主存上执行的速度。这是因为CPU访问寄存器速度远大于主存。
    CPU Cache Memory(高速缓存):由于计算机的存储设备与处理器的运算速度之间有着几个数量级的差距,所以现代计算机系统都不得不加入一层读写速度尽可能接近处理器运算速度的高级缓存,来作为内存与处理器之间的缓冲。将运算时所使用到的数据复制到缓存中,让运算能快速的进行。当运算结束后,再从缓存同步回内存之中,这样处理器就无需等待缓慢的内存读写了。
    RAM-Main Memory(主存/内存):
    当一个CPU需要读取主存的时候,他会将主存中的部分读取到CPU缓存中,甚至他可能将缓存中的部分内容读到他的内部寄存器里面,然后在寄存器中执行操作。当CPU需要将结果回写到主存的时候,他会将内部寄存器中的值刷新到缓存中,然后在某个时间点从缓存中刷回主存。



    Java内存模型抽象结构:每个线程都有一个私有的本地内存,本地内存他是java内存模型的一个抽象的概念。它并不是真实存在的,它涵盖了缓存、写缓冲区、寄存器以及其他的硬件和编译器的优化。本地内存中它存储了该线程以读或写共享变量拷贝的一个副本。

    从更低的层次来说,主内存就是硬件的内存,是为了获取更高的运行速度,虚拟机及硬件系统可能会让工作内存优先存储于寄存器和高速缓存中,java内存模型中的线程的工作内存是CPU的寄存器和高速缓存的一个抽象的描述。而JVM的静态内存存储模型它只是对内存的一种物理划分而已。它只局限在内存,而且只局限在JVM的内存。

1、Java内存模型-同步八种操作



    lock(锁定) :作用于主内存变量,把一个变量标识为一条线程独占状态
    unlock(解锁) : 作用于主内存的变量,把一个处于锁定状态的变量释放出来,释放后的变量才可以被其他线程锁定
    read(读取) : 作用于主内存的变量,把一个变量值从主内存传输到线程的工作内存中,以便随后的load动作使用
    load(载入) :作用于工作内存的变量,它把read操作从主内存中得到的变量值放入工作内存的变量副本中
    use(使用) :作用于工作内存的变量,把工作内存中的一个变量值传递给执行引擎
    assign(赋值) : 作用于工作内存的变量,它把一个从执行引擎接收到的值赋值给工作内存的变量
    store(存储) : 作用于工作内存的变量,把工作内存中的一个变量的值传送到主内存中,以便随后的write操作
    write(写入) :作用于主内存的变量中,它把store操作从工作内存中一个变量的值传送到主内存的变量中

2、Java内存模型-同步规则

    如果要把一个变量从主内存中复制到工作内存,就需要按顺序的执行read和load操作,如果把变量从工作内存中同步回主内存中,就要按顺序的执行store和write操作。但java内存模型只要求上述操作必须按顺序执行,而没有保证必须是连续执行
    不允许read和load、store和write操作之一单独出现
    不允许一个线程丢弃它的最近assign的操作,即变量在工作内存中改变了之后必须同步到主内存中
    不允许一个线程无原因的(没有发生过任何assign操作)把数据从工作内存同步回主内存中
    一个新的变量只能在主内存中诞生,不允许在工作内存中直接使用一个未被初始化(load或assign)的变量。即就是对一个变量实施use和store操作之前,必须先执行过了assign和load操作。
    一个变量早同一时刻只允许一条线程对其进行lock操作,但lock操作可以被同一条线程重复执行多次,多次执行lock后,只有执行相同次数的unlock操作,变量才会被解锁。lock和unlock必须是成对出现。
    如果对一个变量执行lock操作,将会清空工作内存中此变量的值,在执行引擎使用这个变量前需要重新执行load或assign操作初始化变量的值。
    如果一个变量事先没有被lock锁定,则不允许对它执行unlock操作,也不允许unlock一个被其他线程锁定的变量
    对一个变量执行unlock操作之前,必须先把此变量同步到主内存中(执行store和write操作)

Guess you like

Origin www.cnblogs.com/tc971121/p/12080409.html