Java concurrency tutorial ambiguity
Hi I was reading the tutorial on concurrency at http://java.sun.com/docs/books/tutorial/essential/concurrency/atomic.html and I found a passage that I is unclear to me. The article states that: "Using volatile variables reduces the risk of memory consistency errors". Does it reduce the risk or eliminate it altogether for the given variable. In which case would it not eliminate the risk of of memory consistency errors.
Thanks for your help,
Raphael
# 1
If a variable is volatile, then every access to that variable will come from the single main memory copy, and any read that occurs after another write--even if the read and write are in different threads--will see that write's value.
Of course, if the variable is a reference to an object whose members are not volatile or doesn't use syncing to ensure that it's in a valid state, then even though the volatile variable itself will always be valid and up-to-date, there's no guarantee about the overall state of the object it refers to.
Maybe that's what they meant. Or maybe it was just sloppy wording.
jverda at 2007-7-12 19:44:29 >

# 2
a bit more information
You will always see the most recently writen version of a volatile reference however as said this does not necessarily apply to the feilds of a object it references and gives you no guarentees about read/update/write operations.
So, at least for primatives or the object referenced, you can use it in a thread safe manner if all writes to it are unconditional and that care is taken to ensure that immutable objects referfenced by it are fully constructed before being assigned to it (nasty)
I think the concept is this:
Any (non-volatile) variables written, by thread A, before a write to a volatile variable are guarenteed to be seen by thread B after a read from the same volatile. However you can still get inconsitent state as the variables written by A may have changed since (they arn't volatile after all) and you may see thier new value or the value from just before the write to the volatile.
matfud