[concurrency-interest] Visibility of lazy intialisation

Mike Quilleash mike.quilleash at subexazure.com
Tue Jul 18 09:05:10 EDT 2006


I have a static singleton that I want to lazy initialise.
 
Currently the code I have looks like this
 
 
private static Helper helper = null;
 
public synchronized static Helper getHelper() throws HelperException
{
  if ( helper == null )
  {
    Helper localHelper = new Helper();
    localHelper.initialise();
    helper = locaHelper;
  }
 
  return helper;
}
 
 
The initialise() function may throw an exception which I want to
propogate up and catch somewhere higher.  I'm wondering if it definately
the case that the helper variable will NOT be null if an exception is
thrown in the Helper constructor or initialise().  Can the JVM/OS/CPU
reorder the instructions so that helper is set before initialise is
executed?
 
Should I instead be using one of the static initialise idions, like a
static class?  My problem would then become how to catch the initialise
when it is in a static code block.
 
Cheers for any help/advice.
 

 This e-mail is bound by the terms and conditions described at http://www.subexazure.com/mail-disclaimer.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060718/906d3156/attachment.html 


More information about the Concurrency-interest mailing list