Re: what is the point of volatile?
- From: Daniel Pitts <newsgroup.spamfilter@xxxxxxxxxxxxxxxxxxx>
- Date: Thu, 22 Nov 2007 09:55:20 -0800
Lew wrote:
apm35@xxxxxxxxxxxxxxxxxx wrote:It's worth pointing out that volatile doesn't mean atomic: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.
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-concurrency-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/>
.
- Follow-Ups:
- Re: what is the point of volatile?
- From: Knute Johnson
- Re: what is the point of volatile?
- References:
- what is the point of volatile?
- From: apm35
- Re: what is the point of volatile?
- From: Lew
- what is the point of volatile?
- Prev by Date: Re: what is the point of volatile?
- Next by Date: Re: write read string data
- Previous by thread: Re: what is the point of volatile?
- Next by thread: Re: what is the point of volatile?
- Index(es):
Relevant Pages
|