David M. Lloyd wrote:
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.
Yeah, sure :) But we saw that so often ... I would prefer to protect the
framework users against those kind of mistakes rather than receive
hundred of mails saying 'hey, the code is not thread safe"' !!!
Because even if you know that you should not do atomic operation on
volatile variables, people will do that mistake. (again, include me into
the 'people' group).
- DML
--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org