See below ...

Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
http://www.javaspecialists.eu
Tel: +30 69 72 850 460
Skype: kabutz 



Jason Morris wrote:
>> The volatile is important of course, due to the double-checked-locking 
>> visibility bug.
>>
>>     
> The first method you're likely to call is "getInstance()", which means 
> putting 
> the loading in that method wouldn't buy you anything.
>
> Eliminate the overhead of the "if" and "volatile" (which is almost as 
> expensive 
> as half a synchronized for each read):
>
>   
Your second solution is great - if you don't mind the instance being 
created the first time the class is loaded.  Sometimes you don't want 
that, for example if you have other static methods that you might want 
to invoke without necessarily creating an instance of the class.  For 
example, look at java.awt.Toolkit, which contains a static getProperty() 
method.  (And yes, it is called many times, 168 to be precise, in the 
JDK 6).

The nice thing with your second solution is that the call to the 
overridden method is inlined at runtime if you use the server hotspot 
compiler.  If you use the -client hotspot, then my approach seems faster 
on my laptop, since it won't inline the method call.

I don't think that volatile is nearly as expensive as that.  More like 
1/100th of the cost of synchronized.  There is still a cost, of course, 
and you also introduce extra contention with my volatile approach, which 
you don't do in yours.

Was the wrapper approach your own idea, Jason?  I could not find 
references to it on the web.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
-~----------~----~----~----~------~----~------~--~---

Reply via email to