《Java并发编程实战》笔记-Happens-Before规则

Happens-Before规则

程序顺序规则。如果程序中操作A在操作B之前,那么在线程中A操作将在B操作之前执行。

监视器锁规则。在监视器锁上的解锁操作必须在同一个监视器锁上的加锁操作之前执行。

volatile变量规则。对volatile变量的写入操作必须在对该变量的读操作之前执行。

线程启动规则。在线程上对Thread.start的调用必须在该线程中执行任何操作之前执行。

线程结束规则。线程中的任何操作都必须在其他线程检测到该线程已经结束之前执行,或者从Thread.join中成功返回,或者在调用Thread.isAlive时返回false。

中断规则。当一个线程在另一个线程上调用interrupt时,必须在被中断线程检测到interrupt调用之前执行(通过抛出InterruptedException,或者调用isInterrupted和Interrupted)。

终结器规则。对象的构造函数必须在启动该对象的终结器之前执行完成。

传递性。如果操作A在操作B之前执行,并且操作B在操作C之前执行,那么操作A必须在操作C之前执行。

以下是英文描述happens-before,The happens-before relation defines when data races take place.

  • An unlock on a monitor happens-before every subsequent lock on that monitor.
 
  • A write to a volatile field happens-before every subsequent read of that field.
 
  • A call to start() on a thread happens-before any actions in the started thread.
 
  • All actions in a thread happen-before any other thread successfully returns from a join() on that thread.
 
  • The default initialization of any object happens-before any other actions (other than default-writes) of a program.
原文地址:https://www.cnblogs.com/birdstudio/p/6644258.html