Pascal schrieb:
> 
> Per the java spec, "The load, store, read, and write actions on volatile
> variables are atomic, even if the type of the variable is double or
> long."  Problem is, atomic in Java does not mean thread safe.  I learned
> this lesson the hard way back when I wrote my own thread factory.
> Atomic in java means you will never get a mangled value.  That is to say
> that you won't see the value while it's in the process of being
> changed.  For example, if you have a non-volatile long you may get half
> of it in one state and the other half in a different state.
> 
> Bottom line, ++'ing a volatile is always atomic, just not thread safe.
> I believe that is what you meant to ask yes?

Nope, reading and writing it is atomic, but ++'ing is reading *and* 
writing - as a context switch can happen in between, ++'ing is not 
atomic - and therefore not thread safe either.

but the description above is right - as writes are atomic, you won't get
mangled values.

IMO volatile without synchronized is only a good idea for booleans
(volatile booleans will be reread as often as possible, non-volatile
booleans may be cached by each thread till the next enter/exit of a
synchronized block) or for longs/doubles (for preventing mangled
values), but not for ints.

mihi

_______________________________________________
devl mailing list
devl at freenetproject.org
http://hawk.freenetproject.org:8080/cgi-bin/mailman/listinfo/devl

Reply via email to