That article is saying that if you have code like
foo = new MyInt(7);
in a class that has a field
MyInt foo;
then the instructions that amount to
(reference to new object).x = 7;
foo = (reference to new object);
could be swapped over as some kind of optimisation. This will never change the behaviour of the thread that's running this code, but it's possible that some other thread will be able to read foo
after the line
foo = (reference to new object);
but before the line
(reference to new object).x = 7;
in which case it would see foo.x
as 0
, not 7
. That is to say, that other thread could run
int bar = someObject.getFoo().getValue();
and end up with bar
equal to 0
.
I've never seen anything like this happen in the wild, but the author seems to know what he's talking about.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…