T
Timo Nentwig
Hi!
I just read
http://www.javaworld.com/javaworld/jw-11-2001/jw-1116-dcl.html (which
was written back in 2001!). Well, I've seen a lot of Double-Check Lock
(DCL) code and can't remember to ever have seen any usage of
ThreadLocal. I am wondering whether the getInstance() problem cannot be
solved by volatile (I confess that I yet didn't understand volatile
completely). So, my getInstance() (or getResource() as it's named in
the article) would look like this:
class VolatileDCL {
private static volatile Resource resource = null;
public static Resource getInstance() {
if(resource == null)
{
synchronized(this)
{
if(resource==null)
resource = new Ressource();
}
}
return resource;
}
Seriously, how often does bytecode reordering lead to problems in the
real world? I'm really surprised that virtually no developer does seem
to care...
I just read
http://www.javaworld.com/javaworld/jw-11-2001/jw-1116-dcl.html (which
was written back in 2001!). Well, I've seen a lot of Double-Check Lock
(DCL) code and can't remember to ever have seen any usage of
ThreadLocal. I am wondering whether the getInstance() problem cannot be
solved by volatile (I confess that I yet didn't understand volatile
completely). So, my getInstance() (or getResource() as it's named in
the article) would look like this:
class VolatileDCL {
private static volatile Resource resource = null;
public static Resource getInstance() {
if(resource == null)
{
synchronized(this)
{
if(resource==null)
resource = new Ressource();
}
}
return resource;
}
Seriously, how often does bytecode reordering lead to problems in the
real world? I'm really surprised that virtually no developer does seem
to care...