A. Bolmarcich wrote:
On 2007-01-31, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
This is why I keep asking these questions because I get different
answers. Can you explain what is meant in the documentation then by:
JLS 17.4.4 Synchronization Order
...
A write to a volatile variable (?8.3.1.4) v synchronizes-with all
subsequent reads of v by any thread (where subsequent is defined
according to the synchronization order).
Note that "subsequent is defined according to the synchronization order".
Earlier in the section synchronization order is defined of an execution.
Different executions may have different synchronization orders.
According to the section 17.4.5 Happens-before Order: "It should be noted
that the presence of a happens-before relationship between two actions
does not necessarily imply that they have to take place in that order in
an implementation. If the reordering produces results consistent with a
legal execution, it is not illegal."
If there is nothing that forces the ordering of an execution to be that
the write action is before the read, then the read may occur before the
write.
Your previous question was: "I want to know if making the variable
volatile will guarantee that the second thread always sees the latest
integer created by the first thread." The answer depends on exactly
what you mean by "latest". The second thread will always see the most
recent write action by the first thread that has completed. However,
unless you have another synchronization action to force what you
consider to be the most recent write to be done before the read, the
answer is no.
Thanks very much for your response. The two actions are independent and
I do not want to effect when they occur. I just want to ensure that if
other thread will have the latest value.