Re: Synchronization Question
Thomas Pornin wrote:
Also, I do not think that it provides atomicity of writes on
volatile-qualified 'double' and 'long', unlike an implied synchronized
block (which is why I use the term "almost" above).
I refer again to Java Concurrency in Practice, Brian Goetz et al,
Page 36, section 3.1.2
"When a thread reads a variable without synchronization, it may see a
stale value, but at least it sees a value that was actually placed there
by some thread rather than some random value. This safety guarantee is
called 'out-of thin-air safety'.
Out-of-thin-air safety applies to all variables, with one exception:
64-bit numeric variables (double and long) that are not declared
volatile (see Section 3.1.4). The Java Memory Model requires fetch and
store operations to be atomic, but for nonvolatile long and double
variables, the JVM is permitted to treat a 64-bit read or write as two
separate 32-bit operations. If the reads and writes occur in different
threads, it is therefore possible to read a nonvolatile long and get
back the high 32 bits of one value and the low 32 bits of another.
Thus, even if you don't care about stale values, it is not safe to use
shared mutable long and double variables in multithreaded programs
unless they are declared volatile or guarded by a lock."
A bit round about but longs and doubles declared volatile are indeed
atomic as to fetch and store.
--
Knute Johnson
email s/nospam/knute2009/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access