I want atomic reads and atomic writes to Integer (not int) and others as follows:
volatile Double a; // not double
volatile Long b; // not long
Integer c; // not int
Boolean d; // not boolean
If not, how would I make them atomic?
Edit: As a clarification, I do not need an atomic operation that combines read and write. I just need the read to be atomic and the write to be atomic, separately.
Edit2: @Peter Lawrey, @PeterLawrey, This is ironic:
Double a;
a = 0.5d;
Here the write is atomic because a
is a reference. While in this
double a;
a = 0.5d;
the write is not be guaranteed to be atomic. And yet the Double
wraps a double
. Of course I would not expect the same to apply to the wrapped contents of arbitrary objects that have references read and written atomicly.
Edit3: Additional note, I originally wanted to get atomic reads and writes thinking that atomicity ensures consistency across threads. I found that it doesn't and that you still need volatility.