Ulf Zibis said the following on 10/08/09 21:58:
Am 08.10.2009 12:59, David Holmes - Sun Microsystems schrieb:
It's a memory model issue. The code is like this:
public String getName() {
if (name == null)
name = getName0();
return name;
}
but in theory, accoridng to the JMM experts, it could act as if it
does this:
public String getName() {
String tmp1 = name; // sees null
String tmp2 = name; // sees non-null
if (tmp2 == null)
tmp1 = name = getName0();
return tmp1;
}
imagine the temporaries are registers.
Oops, java programming is not simple as it looks like. Can you give me a
link, where I can read more about that subject / JMM?
David, thanks for your explicit explanation.
The JMM is mostly defined in Chapter 17 of the Java Language
Specification Third Edition, but it has little pieces scattered in other
places in the spec (eg treatment of final fields). For more gory details
than you would want to know check out this site:
http://www.cs.umd.edu/~pugh/java/memoryModel/
For the record I disagree with this particular case as I think returning
null would violate program order. But the fix is benign so not worth
arguing over. And the argument is too difficult to make. :)
And as this is getting very OT that's enough of that :)
Cheers,
David