On 3/07/2010, at 2:13 PM, Adrian Crum wrote: > --- On Fri, 7/2/10, David E Jones <[email protected]> wrote: >> On Jul 2, 2010, at 7:11 PM, Adrian Crum wrote: >> >>> Because I've done it myself. ;-) >> >> Wait, do you mean that in this case you did the actual >> copying and pasting? Well... I guess that would be one way >> to know for sure! > > Btw, I'm not implying the code Scott is working on was copy-and-paste code - > I was just making a generalized statement about where my head is at lately. > > When I first got involved with Java, I would copy and paste > double-checked-locking code blocks because I assumed they were necessary. I > didn't bother to research why they were there. If I had, I would have learned > that they don't work and there are better ways to do synchronization. That is > only one example of my uninformed copy-and-paste code development - there are > more.
This has always bothered me, as far as I can tell double-checked locking DOES
work just fine, the only time it doesn't is when you do the following:
private static Object object;
public static Object getObject() {
if (object == null) {
synchronized (Some.class) {
if (object == null) {
object = new Object();
}
}
}
// potentially not fully initialized!!
return object;
}
But we very rarely use the pattern in that manner. What we do usually is
initialize the object and then in an additional statement we assign it to the
variable being checked. The potential for returning partially initialized
objects is the concurrency book's biggest argument against the pattern I don't
often see that potential in the ways that OFBiz uses it.
Regards
Scott
smime.p7s
Description: S/MIME cryptographic signature
