Art Java concurrent programming (6) happens-before

About happens-before

In the JMM, if a result of executing the operation requires another operation to be seen, it must be present happens-before relation between these two operations
happens-before rule
(1) the program sequence rules : a thread for each operation, It happens-before in any subsequent operation of the thread.
(2) monitoring the lock rule : unlock a lock, happens-the before subsequent lock to lock.
(. 3) volatile variable rules : to write a volatile domain, the happens-before in any subsequent reading of the volatile domain
(4) transitive : if A the happens before- B, and B the happens before- C, then A happens- the before C
has a happens-before relationship between the two operations, does not mean that a pre-operation must be performed prior to an operation after! before it happens-before requires only one operation (executed result) after the operation of a visible and sequenced previous operation before the second operation.
Here Insert Picture Description

happens-before-defined

(1) If a further happens-before operating the operation, the execution result of the operation of a second operation will be seen, the first order of execution and a discharge operation before the second operation
(2) two operations between the happens-before relationship, it does not mean that the specific order of implementation of the Java platform must be specified in the happens-before relationship is performed. If the results after the re-ordering, consistent with the results as happens-before relationship is performed, then this reordering is not illegal (JMM allow such reordering)

happens-before rule

(1) The program sequence rules : each operation a thread, happens-before any subsequent operation to the thread.
(2) monitor lock rules : unlocking a lock, happens-before subsequent locking of the lock
(. 3) volatile variable rules : to write a volatile domain, a domain volatile read this happens-before any subsequent to .
Here Insert Picture Description
(4) transitive : if A happens-before B, and B happens-before C, then the happens-before C A
(. 5) Start () rule : If the thread A executes operations ThreadB.start () (start thread B), then a thread
ThreadB.start () operation happens-before any action in the thread B.
Here Insert Picture Description
A thread in advance to modify the shared variable, then start letting the thread B, then thread B will first read the shared variable (A modification of the B-visible).
(. 6) the Join () rule : If the thread A executes operations ThreadB.join () returns successfully, then any of the following happens-before thread B to the thread A in the () operation is successful return from ThreadB.join.
Here Insert Picture Description

as-if-serial

No matter how reordering (compiler and processor in order to improve the degree of parallelism) execution result (single-threaded) program can not be changed. Compiler, runtime and processors must comply with as-if-serial
compiler and processor will not rely on the existence of the relationship between the operation data do reorder, reordering because it will change the result. However, the absence of data dependencies between operations if these operations could be reordered compilers and processors.

    double pi=3.14; A
    double r=1.0; B
    double area=pi*r*r; C

C does not ahead of AB, A and B may occur reordering
the results of the single-threaded program semantics guarantee as-if-serial not changed, happens-before relation guarantee the results of a multithreaded program is not changed correctly synchronized .

Published 24 original articles · won praise 1 · views 546

Guess you like

Origin blog.csdn.net/qq_45366515/article/details/105129536