For Programmers: Free Programming Magazines  


Home > Archive > Java Help > November 2007 > what is the point of volatile?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author what is the point of volatile?
apm35@student.open.ac.uk

2007-11-22, 10:15 pm

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
to write a synchronized getter and setter. But it only works for types
that can be updated atomically. This sounds a bit tricky to me....

Regards,

Andrew Marlow
Lew

2007-11-22, 10:15 pm

apm35@student.open.ac.uk wrote:
> 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
> to write a synchronized getter and setter. But it only works for types
> that can be updated atomically. This sounds a bit tricky to me....


The point of volatile is to provide a kind of lightweight synchronization.

I don't know why you call it "lazy". Nor is it limited to private members;
volatility is orthogonal to access.

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.

--
Lew
Daniel Pitts

2007-11-22, 10:15 pm

Lew wrote:
> apm35@student.open.ac.uk wrote:
>
> The point of volatile is to provide a kind of lightweight synchronization.
>
> I don't know why you call it "lazy". Nor is it limited to private
> members; volatility is orthogonal to access.
>
> 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.
>

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/wordpres...cy-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.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Knute Johnson

2007-11-22, 10:15 pm

Daniel Pitts wrote:
> Lew wrote:
> 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/wordpres...cy-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.


volatile is useful for any variable that will be read in one or many
threads and assigned in another.

--

Knute Johnson
email s/nospam/knute/
Daniel Pitts

2007-11-22, 10:15 pm

Knute Johnson wrote:
> Daniel Pitts wrote:
>
> 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".
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Knute Johnson

2007-11-22, 10:15 pm

Daniel Pitts wrote:
> Knute Johnson wrote:
> 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".


A little complicated for us simpletons but yes :-).

--

Knute Johnson
email s/nospam/knute/
Roedy Green

2007-11-24, 4:35 am

On Thu, 22 Nov 2007 08:22:59 -0800 (PST), apm35@student.open.ac.uk
wrote, quoted or indirectly quoted someone who said :

>Can anyone tell me what the value is of 'volatile' please?


see http://mindprod.com/jgloss/volatile.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
apm35@student.open.ac.uk

2007-11-27, 7:24 pm

On 22 Nov, 16:38, Lew <l...@lewscanon.com> 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
> 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

2007-11-27, 10:16 pm

apm35@student.open.ac.uk wrote:
> 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/me...kedLocking.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/Initia...nd_holder_idiom>
that doesn't rely on volatile.

--
Lew
brian@briangoetz.com

2007-11-29, 7:16 pm

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/j...j-jtp06197.html

On Nov 27, 5:07 pm, ap...@student.open.ac.uk wrote:
> On 22 Nov, 16:38, Lew <l...@lewscanon.com> wrote:
>
>
> 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


apm35@student.open.ac.uk

2007-11-29, 7:16 pm

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 ;-)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com