On 05/14/2008 12:11 PM, Emmanuel Lecharny wrote:
David M. Lloyd wrote:
On 05/14/2008 10:55 AM, Emmanuel Lecharny wrote:
Sangjin Lee wrote:
Hmm... Could you share a simple example of a non-volatile variable
that
respects the volatile contract?
any boolean.
This is incorrect. Changes made to a boolean that is not volatile may
never become visible in another thread.
Sorry, I didn't read the question carefully. You are right David.
What I meant is that a volatile boolean is clearly protected, as you
can't apply simple opration like ++ or -- on it. I don't know about :
volatile boolean b = Boolean.TRUE;
...
b = !b; // Might fails if used in a concurrent portion of the code.
Right, you do not get free atomic operations with volatile. That said, I
can't think of any case where it's not safe to use volatile for properties
that are read and written with getters and setters - you generally can't
(and shouldn't attempt to) do atomic operations with such properties
anyway. Trying to do so is often indicative of a design flaw.
- DML