>> Can anyone tell me what the value is of 'volatile' please? It seems to
>> me to be a lazy way of protecting a private data member without having
[quoted text clipped - 11 lines]
> is not so for non-volatile variables. That is the new (as of 5) memory
> model for Java.
It's worth pointing out that volatile doesn't mean atomic:
volatile int a;
++a; // Still two separate accesses to a
I didn't fully understood volatile until I read about it in the book
Java Concurrency In Practice
<http://virtualinfinity.net/wordpress/technical-book-recommendations/java-concurr
ency-in-practice/>
The only real use I've found for it is for a simple
you-should-shut-down-now flag to a tight-looped thread. That thread
only needs to read, and other threads only need to write.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Knute Johnson - 22 Nov 2007 19:00 GMT
>>> Can anyone tell me what the value is of 'volatile' please? It seems to
>>> me to be a lazy way of protecting a private data member without having
[quoted text clipped - 25 lines]
> you-should-shut-down-now flag to a tight-looped thread. That thread
> only needs to read, and other threads only need to write.
volatile is useful for any variable that will be read in one or many
threads and assigned in another.

Signature
Knute Johnson
email s/nospam/knute/
Daniel Pitts - 22 Nov 2007 19:15 GMT
>>>> Can anyone tell me what the value is of 'volatile' please? It seems to
>>>> me to be a lazy way of protecting a private data member without having
[quoted text clipped - 28 lines]
> volatile is useful for any variable that will be read in one or many
> threads and assigned in another.
To further abstract...
volatile is useful if any *one* thread doesn't both read and write that
variable. Any thread can do one and not the other, and the outcome is
"predictable".

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Knute Johnson - 22 Nov 2007 20:43 GMT
>>>>> Can anyone tell me what the value is of 'volatile' please? It seems to
>>>>> me to be a lazy way of protecting a private data member without having
[quoted text clipped - 33 lines]
> variable. Any thread can do one and not the other, and the outcome is
> "predictable".
A little complicated for us simpletons but yes :-).

Signature
Knute Johnson
email s/nospam/knute/
> Reads from a volatile member are guaranteed to see previous writes to that
> variable. In fact, reads from a volatile variable guarantee that all writes
> prior to the latest write to that variable are visible. This is not so for
> non-volatile variables. That is the new (as of 5) memory model for Java.
Thanks for that. I was not aware the guarantee was new to java 5. The
book I am learning from (Thinking in Java by Bruce Excel) was in its
final stages as java 5 came out, so although it does mention java 5 I
did not fully get this point. This does indeed make it convenient to
make a cheap check which would otherwise be done (more expensively)
using a private data member and synchronized getters and setters.
I have made some other enquires and apparently this change was made so
that double checking locking could be fixed. Can anyone else here
confirm that please?
-Andrew Marlow
Lew - 28 Nov 2007 02:07 GMT
> I have made some other enquires and apparently this change was made so
> that double checking [sic] locking could be fixed. Can anyone else here
> confirm that please?
Hmm, I just did a quick search and found this within about 60 seconds.
<http://en.wikipedia.org/wiki/Double-checked_locking>
GIYF.
The search also turned up the canonical link for how it was broken before the
change in the Java memory model:
<http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html>
Brian Goetz's seminal /Java Concurrency in Practice/,
<http://www.javaconcurrencyinpractice.com/>
discusses this idiom
However, there is some evidence that even with volatile's new semantics we
haven't completely solved the fundamental problems with double-checked
locking. In the case of lazy initialization a solution exists
<http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom>
that doesn't rely on volatile.

Signature
Lew
brian@briangoetz.com - 29 Nov 2007 15:40 GMT
Fixing double-checked locking was an explicit NON-GOAL for the new
volatile semantics.
Whoever told you that, this is a reliable indication that you should
be very skeptical about anything else they say :)
More on volatile:
http://www.ibm.com/developerworks/java/library/j-jtp06197.html
On Nov 27, 5:07 pm, ap...@student.open.ac.uk wrote:
> > Reads from a volatile member are guaranteed to see previous writes to that
> > variable. In fact, reads from a volatile variable guarantee that all writes
[quoted text clipped - 13 lines]
>
> -Andrew Marlow
apm35@student.open.ac.uk - 29 Nov 2007 17:21 GMT
On 29 Nov, 15:40, br...@briangoetz.com wrote:
> Fixing double-checked locking was an explicit NON-GOAL for the new
> volatile semantics.
>
> Whoever told you that, this is a reliable indication that you should
> be very skeptical about anything else they say :)
It was wikipedia. Here is the quote:-
----
As of J2SE 5.0, this problem has been fixed. The volatile keyword now
ensures that multiple threads handle the singleton instance correctly.
This new idiom is described in [2]:
----
Just goes to show one mustn't believe everything that is on
wikipedia ;-)