JMM learning articles

Key questions 1. concurrent programming areas

Communication between 1.1 thread

It refers to the communication thread what mechanism to exchange information between threads. In programming, the communication mechanism between the two threads, shared memory and message passing.
In the shared-memory concurrency model, the common state shared program between the threads between the thread by writing - implicitly communicate, typical of shared memory communication is to communicate by sharing a common target state read memory.
In concurrency model messaging, there is no common thread between the state, must be explicitly communicate by transmitting explicit messages between threads, in a typical embodiment is the java messaging wait () and notify ().

Thread synchronization between 1.2

It refers to the relative order of the synchronization mechanism between the operating program for controlling the occurrence of different threads.
In the shared-memory concurrency model, explicit synchronization is performed. The programmer must explicitly specify a method or a piece of code required to perform mutual exclusion between threads.
In concurrency model's messaging, since the transmission message before receiving the message must therefore implicit synchronization is performed.

2.Java memory model --JMM

  Java programs that need to run in a Java virtual machine above, Java memory model (Java Memory Model, JMM) is a kind of standard memory model in line, shielding the differences in access to a variety of hardware and operating systems to ensure that the Java program in each under kinds of platform access to the memory mechanism can ensure consistent results and specifications.

  • Java concurrency uses a shared memory model

2.1 modern computer memory model

Concurrency issues in the physical computer, the situation is complicated by physical problems and opportunities to the virtual machine, there are many similarities, physical machine concurrent treatment options for virtual machine also has considerable reference value.

One important source of complexity is the vast majority of computing tasks can not rely on processor "computing" can be done, at least a processor to interact with memory, such as read the operation data, stores calculation results, the I / O operations are difficult to eliminate (not only by the register to complete all computing tasks). Computer early cpu and memory speed is about the same, but in modern computers, the cpu command speed far exceeds the memory access speed, since the operation speed of the computer processor and memory device with a gap of several orders of magnitude, so modern adding a layer of the computer system had to read and write speed as close as possible processor speed cache (cache) memory as a buffer between the processor: copy operation will need to use the data to the cache, so that operation can quickly, when in operation after the end of sync back from the cache memory, so the processor without waiting for slow memory read and write.

 

 JVM when taking into account in the design, JAVA thread if each read and write variables directly operate the main memory, the greatest impact on performance, so each thread has its own working memory, working memory variables in the main memory copy, the thread of the variables read and write operations in the working memory directly, and not directly to the operation of the main variables in memory. But this will be a problem when a thread modified their working memory variables, other threads are invisible, causes the thread to the problem of insecurity. Because JMM developed a set of standards to ensure developers at the time of writing multithreaded programs can control when memory will be synchronized to the other thread.

  Memory interoperability

   There are eight kinds of interworking memory, virtual machine implementation must ensure that each operation is atomic, not in minutes (for double and long type variable for, load, store, read and write operations on some platforms allow exceptions )

    • lock (lock): acting on the main memory variable, a variable that identifies the thread to exclusive state
    • unlock (unlock): the role of main memory variable that the variable is in a locked state is released, after the release of the variables can be locked other threads
    • read (read): acting on the main memory variable, the value of a variable which is transmitted from the main memory into the working memory of the thread, for subsequent use load operation
    • load (load): the role of working memory variable that the read operation from the main memory into the working memory variable
    • use      (使用):作用于工作内存中的变量,它把工作内存中的变量传输给执行引擎,每当虚拟机遇到一个需要使用到变量的值,就会使用到这个指令
    • assign  (赋值):作用于工作内存中的变量,它把一个从执行引擎中接受到的值放入工作内存的变量副本中
    • store    (存储):作用于主内存中的变量,它把一个从工作内存中一个变量的值传送到主内存中,以便后续的write使用
    • write  (写入):作用于主内存中的变量,它把store操作从工作内存中得到的变量的值放入主内存的变量中

  JMM对这八种指令的使用,制定了如下规则:

    • 不允许read和load、store和write操作之一单独出现。即使用了read必须load,使用了store必须write
    • 不允许线程丢弃他最近的assign操作,即工作变量的数据改变了之后,必须告知主存
    • 不允许一个线程将没有assign的数据从工作内存同步回主内存
    • 一个新的变量必须在主内存中诞生,不允许工作内存直接使用一个未被初始化的变量。就是怼变量实施use、store操作之前,必须经过assign和load操作
    • 一个变量同一时间只有一个线程能对其进行lock。多次lock后,必须执行相同次数的unlock才能解锁
    • 如果对一个变量进行lock操作,会清空所有工作内存中此变量的值,在执行引擎使用这个变量前,必须重新load或assign操作初始化变量的值
    • 如果一个变量没有被lock,就不能对其进行unlock操作。也不能unlock一个被其他线程锁住的变量
    • 对一个变量进行unlock操作之前,必须把此变量同步回主内存

  JMM对这八种操作规则和对volatile的一些特殊规则就能确定哪里操作是线程安全,哪些操作是线程不安全的了。但是这些规则实在复杂,很难在实践中直接分析。所以一般我们也不会通过上述规则进行分析。更多的时候,使用java的happen-before规则来进行分析。

 



Guess you like

Origin www.cnblogs.com/123-shen/p/11351519.html