Java concurrent appreciated from another angle

  Armed with Sequential Consistency consistency model, we re-look at java concurrency.

  As we have said, Sequential Consistency leaving only the process of local order. We learned above, since the CPU instruction reordering, multi-level cache memory inconsistencies and other problems, did not provide the hardware level Sequential Consistency, require software developers to implement. Here we look at how to achieve Sequential Consistency in java.

  Sequential Consistency in the foundation of Java, is a happens-before relationship JVM. In the Java Language Specification memory model, it provides happens-before relationship, properly handle happens-before relationship is the foundation of java language correctly implement concurrency. Be apparent that the same code in a thread, the latter actionhappens-before foregoing action. However, it later said, even if there are two action happens-before relationship, not necessarily in that order occurred in the implementation. As long as the order of the reordered to produce a legitimate result, we can accept.

  

 

  Note here that the "legitimate" result does not mean that the "legal" results meet your expectations.

  Rather, if there are two action happens-before relationship, and they do not respect the code happens-before relationship, they do not necessarily happen in that order.

  For example, for two threads simultaneously access data, one thread writes a read operation in another thread of view, there may be out of order.

  

 

  For example look at it. Suppose two threads X and Y together can access two variables A and B, A and B are initially zero.

  Execution thread in X

  A=5

  B=5

  A and B are simultaneously read Y thread (java in fact not simultaneously read two atoms of variables, we can read the first B, then A is read), then there is no possibility to read B = 5 , a = 0 it? intuitively, it is impossible. A first update since X, after updating B, if B were read to the 5, then A should also be 5 fishes.

  A and B are simultaneously read Y thread (java in fact not simultaneously read two atoms of variables, we can read the first B, then A is read), then there is no possibility to read B = 5 , a = 0 it? intuitively, it is impossible. A first update since X, after updating B, if B were read to the 5, then A should also be 5 fishes.

  

image description

 

  But java memory model it clear that this situation is possible. Because a compiler thinks

  A=5

  B=5

  with

  B=5

  A=5

  Well there is no difference, so it does not matter executed first, so drastic swap the order.

  

image description

 

  So in order to get the results you want, in the programming required to establish the correct happens-before relationship. The established method can refer java language specification.

  For example java language specification to write a predetermined field of volatile, happens-before field of the subsequent read. After determining happens-before relationship, not only for the value of volatile field immediately visible to all threads, also limits access to reorder the field operations.

 

Guess you like

Origin www.cnblogs.com/qfjavabd/p/10983855.html
Recommended