Java Reference
In-Depth Information
If the read of dataReady by thread-2 yields true , then the write of
dataReady by thread-1 happened-before that read. Since the write to data
by thread-1 happens-before the write to dataReady by thread-1, it follows
that it also happens-before the read of dataReady by thread-2, hence the
read of data by thread-2. In short, if thread-2 sees dataReady is true then
it is guaranteed to see the new Data object created by thread-1. This is
true because dataReady is a volatile variable, [2]
even though data is not
itself a volatile variable.
[2] The same would be true if dataReady were not volatile but instead had synchronized get and set
methods.
Finally, note that the actual execution order of instructions and memory
accesses can be in any order as long as the actions of the thread appear
to that thread as if program order were followed, and provided all values
read are allowed for by the memory model. This allows the program-
mer to fully understand the semantics of the programs they write, and
it allows compiler writers and virtual machine implementors to perform
complex optimizations that a simpler memory model would not permit.
This discussion gives you an idea of how the memory model interacts
with multithreading. The full details of the memory model are beyond
the scope of this topic. Fortunately, if you pursue a straightforward lock-
ing strategy using the tools in this topic, the subtleties of the memory
model will work for you and your code will be fine.
 
 
Search WWH ::




Custom Search