Java virtual machine learning - memory model and thread

Java virtual machine learning - memory model and thread

First, why

  Java Virtual Machine specification attempts to define a Java memory model (Java Memory Model, JMM) to shield the various hardware and operating system differences in memory access, in order to achieve let Java programs in a variety of platforms to achieve the same consistent memory access memory effect.

 

 

Second, the main memory and working memory

   The main objective of Java memory model is to define the program access rules for each variable that is stored in the low-level details such as memory and removed from memory variables in a virtual machine jackpot variable.

  Java memory model specifies that all variables are stored in the main memory (Main Memory), each thread has its own working memory (Working Memory), thread all operations on variables (reading, assignments, etc.) must be in working memory performed, not directly read and write main memory variables. Between different threads can not directly access the working memory of the other variable, the variable passed between threads are required to complete the main memory.

 

 

 

 Third, the interaction between the memory

  About concrete between main memory and the working memory of the interactive protocol, namely a variable how to copy from main memory into the working memory, how to synchronize back to the main memory from the working memory implementation details and the like, Java memory model heavy friendship a little eight kinds of operations completed, must ensure that each operation atoms are mentioned below, can not be divided when the virtual machine implementations.

  1. lock (lock): acting on the main memory variable, which is identified as a variable to a thread exclusive state.

  2. 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.

  3. read (read): acting on the main memory variable, which is the value of a variable from the main memory to the working memory of the thread in the working memory, for subsequent use load operation.

  4. load (load): the role of working memory variable, which is obtained in the variable read from the main memory copy of the variable values ​​into the working memory.

  5. use (use): the role of working memory variable, which work to pass a variable value to the memory execution engine.

  6. assign (assignment): the role of working memory variable that receives the value from the execution engine to be attached to the working memory variable.

  7. store (store): the role of working memory variable, the value of memory in which the variable is transferred to the main memory, so that subsequent use of write operations.

  8. write (Write): acting on the main memory variable, which is the value of the variable store operation obtained from the main memory into the working memory variable.

 

 

Four, volatile variables

  1) to ensure the visibility of this variable among threads

    Meaning: When a thread modifies volatile variables, other threads can know

The principle: The thread will modify the volatile variables in working memory, jvm will be the latest value of the brush White memory; thread in the use of volatile variables, reads the value of the start of main memory, the brush into the working memory.

  2) disable command reordering optimization

   Before the statement that volatile variables of code can not be behind the statement, when the statement is executed, the previous statement means that the implementation has been finished, thus forming a "rearrangement instruction memory can not cross the barrier" effect.

 

 

Fifth, the atomicity, ordering and visibility

  Atomicity : java memory model provided read, load, use, assign, store, write operations are atomic, the indivisible operations. For a wide range of atomic, JVM use lock, unlock operation control, these two operations are not directly provided to the user, may be used bytecode instruction the monitorenter, the monitorexit ( Synchronize keywords ) implicitly provided.

  Visibility : java memory model by modifying the variable value of the new value will be synchronized to the main memory, before the read mode variable value from the main memory to refresh achieve visibility. volatile, sychronized, final keywords can ensure visibility. volatile variable differs from common variable, that variable after the volatile modifications will immediately synchronized to the main memory. After synchronized unlock action forces the synchronous variable to main memory. final variable type, after the completion of the constructor initializes, and not the "this" reference pass out, can ensure its visibility.

  Ordering : If this thread is observed, all operations are ordered, if observed in other threads, all operations are unordered. First phrase means "thread of the performance of serial semantics", refers to the last part "instruction rearrangement" and "working memory and the main memory synchronization delay" phenomenon. volatile, orderly operation between sychronized keyword guarantee line. volatile prohibit instruction reordering, sychronized is guaranteed a variable time allowing only one thread lock its operation.

 

 

Sixth, the first principle of occurrence

   java memory model of all orderliness in addition to their volatile and synchronized complete, but also relies on the principle of "first occurrence" (happens-before) a. The principle is to determine whether there is competition, whether the thread safety of the main basis, rely on this principle, the package can solve all the possible problems the possibility of conflict exists between two concurrent operating environment.

  Occurs first partial order relation between two operations defined in the Java memory model, before the first A occurs if the B operation, the operation can affect the operation of the A B produced was observed , "Effect" includes modifying shared memory value of the variable, the message is sent, the calling method.

 

  1. The program sequence rules (Program Order Rule): 

     In a thread, the order code.

   2. The tube-lock rules (Monitor Lock Rule):

     The face of lock with a lock operation after a first unlock operation occurs. Here it must be emphasized that the same lock, and "back" refers to the order in time.

   3. volatile variable rules (Volatile Variable Rule):

     Variable of a volatile write operation occurs after the first read operation in the face of this variable.

   4. Thread start rule (Thread Start Rule):

     start Thread object () method first occurrence in this thread every movement.

  5. 线程终止规则(Thread Termination Rule):

    线程中的所有操作都先行发生于对此线程的终止检测,我们可以通过Thread.join()方法结束、Thread.isAlive()的返回值等手段检测到线程已经终止执行。

  6. 线程中断规则(Thread Interruption Rule):

   对线程interrupt()方法的调用先行于中断线程的代码检测到中断事件的发生,可以通过Thread.interrupted()方法检测到是否有中断发生。      

  7. 对象终结规则(Finalizer Rule):

   一个对象的初始化完成(构造函数执行结束)先行发生于它的finalize()方法的开始。

  8. 传递性(Transitivity):

   如果操作A先行发生于操作B,操作B先行发生于操作C,那就可以得出操作A先行发生于操作C的操作。

 

  时间先后顺序与先行发生原则基本没什么关系,所以衡量并发安全问题时不要受时间顺序的干扰,一切必须以先行发生原则为准。

  

 

七、java线程状态转换

java语言定义了5种线程状态,在任意一个时间点,一个线程有且只有其中一种。

1)新建(New):  新建尚未启动的线程

2)运行(Runnable): 处于该状态的线程有可能正在运行,也可能正在等待CPU为它分配执行时间。

3)阻塞(Blocked): 线程竞争排他锁后并未获取到排他锁时会进入阻塞状态,处于该状态的线程在等待排他锁。

4)无限期等待(Waiting) / 有限期等待(Timed Waiting):

  处于Waiting状态的线程不会被分配CPU执行时间,它们要等待其他线程的显式唤醒。

  处于Timed Waiting状态的线程不会被分配CPU执行时间,不过无需等待被其他线程显式的唤醒,过一段时间它会自动被系统自动唤醒。

5)结束(Terminated): 已终结线程的线程状态。

     

 

学习资料:

  <深入理解java虚拟机>

 

 

Guess you like

Origin www.cnblogs.com/timfruit/p/10338954.html