• Each action in a thread happens-before every subsequent action in that thread.
• 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 volatile.
• 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.
• If an action a happens-before an action b,and b happens before an action c,then a happensbefore c.
• the completion of an object’s constructor happens-before the execution of its finalize method (in the formal sense of happens-before).
前三条,注意every subsequent,每个后续的。
程序顺序规则:如果代码中操作A写在操作B之前,那么同一线程执行时,要先执行操作A,不能重排序。
监视器锁规则:对于同一个 monitor 来说,这个 monitor 的解锁(unlock)要 happens - before 后面对这个监视器的加锁(lock)。
volatile变量规则:对一个 volatile 变量的写操作 happens - before 后续任意对这个变量的读操作。
线程启动规则:一个对象的初始化完成(构造函数执行结束)先行发生于它的 finalize() 方法。
线程结束规则:线程中的任何操作都必须在其他线程检测到该线程已经结束之前执行,或者从Thread.join中成功返回,或者在调用Thread.isAlive时返回false。
传递性:这个最好理解,不用再解释
终结器规则:一个对象的初始化完成(构造函数执行结束)先行发生于它的 finalize() 方法。