Java Reference
In-Depth Information
Listing 16.1. Insufficiently Synchronized Program that can have Surprising Results.
Don't do this.
A
data race
occurs when a variable is read by more than one thread, and written by at least
one thread, but the reads and writes are not ordered by
happens-before
. A
correctlysynchron-
ized program
is one with no data races; correctly synchronized programs exhibit sequential
consistency, meaning that all actions within the program appear to happen in a fixed, global
order.
The rules for
happens-before
are:
Program order rule.
Each action in a thread
happens-before
every action in that thread that
comes later in the program order.
Monitor lock rule.
An unlock on a monitor lock happens-before every subsequent lock on
that same monitor lock.
[3]