Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot

...

Code Block
bgColor#FFCCCC
// ""Double-Checked Locking"" idiom
class Foo { 
  private Helper helper = null;
  public Helper getHelper() {
    if (helper == null) { 
      synchronized(this) {
        if (helper == null) 
          helper = new Helper();
      }    
    }
    return helper;
  }
  // other functions and members...
}

...

EX1: Explicitly synchronized code does not require the use of double-checked locking.

Wiki Markup
*EX2:* ""Although the double-checked locking idiom cannot be used for references to objects, it can work for 32-bit primitive values (e.g., int's or float's). Note that it does not work for long's or double's, since unsynchronized reads/writes of 64-bit primitives are not guaranteed to be atomic."" \[[Pugh 04|AA. Java References#Pugh 04]\]

...

Wiki Markup
\[[API 06|AA. Java References#API 06]\] 
\[[Pugh 04|AA. Java References#Pugh 04]\]
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 609|http://cwe.mitre.org/data/definitions/609.html] ""Double-Checked Locking""

...

FIO36CON42-J. Do not create multiple buffered wrappers on an InputStream      09. Input Output (FIO)      09. Input Output (FIO)Ensure actively held locks are released on exceptional conditions      11. Concurrency (CON)      CON44-J. Do not expect sleep() and yield() methods to have any synchronization semantics