Briefly describe the happen before principle of the Java memory model

Formula: If there is a happen-before relationship between two operations, the result of the previous operation will be visible to the next operation. is the partial ordering relationship between two operations defined in the Java memory model.

Common happen-before rules:

1. Program order rules: each operation in a thread, happens-before any subsequent operation in that thread. (Note: If there is only one thread's operation, then the result of the previous operation will definitely be visible to subsequent operations.)

2. Lock rules: to unlock a lock, happen-before then lock the lock. (Note: the most common of this are synchronized methods and synchronized blocks)

3. volatile variable rules: write to a volatile field, happen-before any subsequent read of this volatile field. This rule is well reflected in the fact that no locking is required in the read operation of CurrentHashMap.

4. Transitive: If A happens-before B, and B happens-before C, then A happens - before C.

5. Thread start rules: The start() method of the Thread object happens-before every action of this thread.

6. Thread termination rules: All operations of the thread happen-before the termination of the thread is detected, which can be detected by the Thread.join() method and the return value of Thread.isAlive() to detect that the thread has terminated execution.

7. Thread interruption rules: The call to the thread interrupt() method happens-before when the code of the interrupted thread detects an interruption.

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325818677&siteId=291194637