Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot (vkp) v1.0

Wiki Markup
Instead of initializing a member object using a constructor, _lazy initialization_ can be used to defer the construction of the member object until an instance is actually required. Lazy initialization also helps in breaking harmful circularities in class and instance initialization, and performing other optimizations \[[Bloch 2005|AA. Java References#BlochBibliography#Bloch 05]\].

A class or an instance method is used for lazy initialization, depending on whether the member object is static. The method checks whether the instance has already been created and, if not, creates it. If the instance already exists, it simply returns it:

...

Wiki Markup
According to Pugh \[[Pugh 2004|AA. Java References#PughBibliography#Pugh 04]\]:

... writes that initialize the Helper object and the write to the helper field can be done or perceived out of order. As a result, a thread which invokes getHelper() could see a non-null reference to a helper object, but see the default values for fields of the helper object, rather than the values set in the constructor.

Even if the compiler does not reorder those writes, on a multiprocessor the processor or the memory system may reorder those writes, as perceived by a thread running on another processor.

...

Wiki Markup
If a thread initializes the {{Helper}} object, a [happens-before relationship|BB. Definitions#happens-before order] is established between this thread and another that retrieves and returns the instance \[[Pugh 2004|AA. Java References#PughBibliography#Pugh 04], [Manson 2004|AA. Java References#MansonBibliography#Manson 04]\].

Compliant Solution (Static Initialization)

Wiki Markup
This compliant solution initializes the {{helper}} field in the declaration of the static variable \[[Manson 2006|AA. Java References#MansonBibliography#Manson 06]\].

Code Block
bgColor#ccccff
final class Foo {
  private static final Helper helper = new Helper();

  public static Helper getHelper() {
    return helper;
  }
}

...

Wiki Markup
Initialization of the static {{helper}} field is deferred until the {{getInstance()}} method is called. This idiom is a better choice than the double-checked locking idiom for lazily initializing static fields \[[Bloch 2008|AA. JavaBibliography#Bloch References#Bloch 08]\].  However, this idiom cannot be used to lazily initialize instance fields \[[Bloch 2001|AA. JavaBibliography#Bloch References#Bloch 01]\].

Compliant Solution (ThreadLocal Storage)

Wiki Markup
This compliant solution (originally suggested by Alexander Terekhov \[[Pugh 2004|AA. Java References#PughBibliography#Pugh 04]\]) uses a {{ThreadLocal}} object to lazily create a {{Helper}} instance.

...

Wiki Markup
*LCK10-EX1:* The noncompliant form of the double-checked locking idiom can be used for 32-bit primitive values (for example, {{int}} or {{float}}) \[[Pugh 2004|AA. Java References#PughBibliography#Pugh 04]\]. Note that it does not work for {{long}} or {{double}} because unsynchronized reads/writes of 64-bit primitives are not guaranteed to be atomic (see guideline [VNA05-J. Ensure atomicity when reading and writing 64-bit values].)

...

Wiki Markup
\[[API 2006|AA. JavaBibliography#API References#API 06]\]
\[[JLS 2005|AA. JavaBibliography#JLS References#JLS 05]\] Section 12.4, "Initialization of Classes and Interfaces"
\[[Pugh 2004|AA. Java References#PughBibliography#Pugh 04]\]
\[[Bloch 2001|AA. Java References#BlochBibliography#Bloch 01]\] Item 48: "Synchronize access to shared mutable data"
\[[Bloch 2008|AA. Java References#BlochBibliography#Bloch 08]\] Item 71: "Use lazy initialization judiciously"
\[[MITRE 2009|AA. Java References#MITREBibliography#MITRE 09]\] [CWE ID 609|http://cwe.mitre.org/data/definitions/609.html] "Double-Checked Locking"

...