2012年/09月/08日

首页回退

HappenBefore

Single thread rule: Each action in a single thread happens-before every action in that thread that comes later in the program order.

Monitor lock rule: An unlock on a monitor lock (exiting synchronized method/block) happens-before every subsequent acquiring on the same monitor lock.

Volatile variable rule: A write to a volatile field happens-before every subsequent read of that same field. Writes and reads of volatile fields have similar memory consistency effects as entering and exiting monitors (synchronized block around reads and writes), but without actually aquiring monitors/locks.

Thread start rule: A call to Thread.start() on a thread happens-before every action in the started thread. Say thread A spawns a new thread B by calling threadA.start(). All actions performed in thread B’s run method will see thread A’s calling threadA.start() method and before that (only in thread A) happened before them.

Thread join rule: All actions in a thread happen-before any other thread successfully returns from a join on that thread. Say thread A spawns a new thread B by calling threadA.start() then calls threadA.join(). Thread A will wait at join() call until thread B’s run method finishes. After join method returns, all subsequent actions in thread A will see all actions performed in thread B’s run method happened before them.

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