> On a 32-bit JVM, is atomicity guaranteed ? I mean: I am guaranteed
> to always read a value that was actually written in that global int by
> another thread or is it possible to read my int in an inconsistent
> state?
Yes. Or on any other JVM -- that atomicity is part of the language spec.
> Am I guaranteed to always read the latest value that has been written
> in that global int by any thread (I understand the answer is "no" but I
> may be confused) ?
No there is definitely no such guarantee.
> If some thread may be reading old values, where do they come from?
Some possible answers:
-- the stale value is in the CPU's internal caches
-- the new value is in memory associated with one processor
which has not yet been propagated to the memory seen
by another processor.
-- the JIT/compiler has generated code to make and use a copy
of the value in A.global instead of going back to the "real"
field on each access.
> On a 32-bit JVM, if I do the same with a *long*, is atomicity
> guaranteed for the long?
No. Again this is part of the JLS, nothing to do with the 32-bitness of the
JVM.
> And now a though one (to grasp for me): is it possible, on a 32-bit
> JVM,
> to have atomicity of a *long* guaranteed, but no guarantee made as
> to which value would a thread actually read? If so, how could one
> achieve such an horror?
I imagine that could be possible if the real CPU and memory system was capable
of issuing 64-bit reads and writes, if the CPU/memory is such that it offers
stronger atomicity guarantees than are required for a conforming JVM, and if
the JIT generated code to take advantage of that. If so then reads/writes
could never "see" invented values, but stale data could still be lurking in the
system's caches and whatnot. Note, though, that this would be an "accidental"
property of the implementation, not something that is guaranteed by the
language.
I could easily be wrong, but I /suspect/ that a current JVM running on a 32-bit
P4 has that property. Please don't take that as any kind of an expert opinion
! It's more an invitation for someone to explain that I'm wrong (again).
> And lastly, isn't AtomicBoolean actually a misnomer? How could you
> read a boolean and have something else than 0 or 1? How could a
> boolean be in an inconsistent state?
I think the "atomic" part of the name refers to the atomic test-and-set
operations rather than to any indivisibility of the value itself. Similarly
for the other "atomic" classes.
-- chris
Thomas Hawtin - 14 Jul 2006 16:41 GMT
>> And lastly, isn't AtomicBoolean actually a misnomer? How could you
>> read a boolean and have something else than 0 or 1? How could a
[quoted text clipped - 3 lines]
> operations rather than to any indivisibility of the value itself. Similarly
> for the other "atomic" classes.
Indeed. According to the spec, volatile is enough to ensure atomic read
and writes for longs and doubles. The AtomicXxxFieldUpdater classes
would be pointless without atomic operations.
A common mistake is to assume that ++ will work atomically on volatile
variables. ++a; is equivalent to a = a + 1; and is not atomic.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/