The concept of threads (three) --- Java memory model JMM

JMM key technical point revolves around multi-threaded atomicity, visibility and orderliness to build.

1, atomicity (Atomicity)

  Atomicity refers to an operation is not interrupted. Even when executed with multiple threads, an action once started, will not be interference from other threads.

  For example, for a static global variable int i, assign the value two threads, thread A assigned the value 1, thread B assigned the value -1. So regardless of the manner in which these two threads, what pace of work, the value of i is either 1 or -1. Between the thread A and thread B are without interference. This is a feature of atomicity, it can not be interrupted.

  But if we do not use int data type and data type using a long, you might not be so lucky. For 32-bit system, the write data is not long type of atom (Data as 64 bits long). That is, if two threads of long type writing (or reading), the results between the threads is interference.

2, visibility (the Visibility)

  Visibility means that when a thread modifies the value of a variable of a shared, other threads can immediately know whether this modification. Obviously, for a serial program, the visibility of the problem does not exist. Because you modify a variable in any one of the steps, the value of this variable is read in a subsequent step, the reading must be the new modified value.

3, orderliness (Ordering)

  Ordering problem may be the hardest to understand three questions in the. For a thread of code execution, we always used to think that the code is executed sequentially from front to back. So understanding can not say completely wrong, because it is within a thread, it does behave like this. However, when the concurrent execution of the program might appear out of order. It gives the visual impression is: EDITORIAL code will be executed later. It sounds weird, is not it? The reason ordering problem is in the implementation of the program may be rearranged instructions, instructions rearranged the order may not be consistent with the original instructions.

Guess you like

Origin www.cnblogs.com/jackcto/p/11954614.html