Hi,
This page here discusses the changes to the
java memory model that were made in Java 5.0.
In
this section the author discusses how a volatile field can be used to guarantee the ordering or reads and writes. Previously I'd thought this was only possible with synchronised. For example, the following code also guarantees that if v == true, then x == 42
My question is this: are there any differences in terms of
thread safety, speed, etc. between the code I've shown above and that shown on the webpage? More generally, what are the advantages of using synchronized versus volatile to guarantee the ordering of reads/writes in a multithreaded program?
Cheers,
DM
[ January 30, 2007: Message edited by: Dan Murphy ]