After thinking about it some more, I am less appalled about this example
than I had been.
For people who feel compelled to write code that doesn't use synchronization
but that depends on the automaticity of reads/writes of ints and refs, the rule
is simple:
* Never read a field twice if that field is being updates without
synchronization
by other threads. If you do read it twice, you have absolutely no guarantees
of any kind about the consistency of those two values. If fact, under some
circumstances, the first read can return a new value and the second
read return
an older value.
The compiler/VM guarantees that if your code reads the variable
once, that all
uses of the value read will be consistent.
This still means that some implementations of the single-check and double-check
idioms don't fit under these rules. But it will be possible to write a correct
implementation if you are very careful.
Still not something I would recommend for the masses.
Bill
-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel
This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:22 EDT