Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 691 Bytes

DoubleCheckedLocking.md

File metadata and controls

24 lines (21 loc) · 691 Bytes

DoubleCheckedLocking

Category: pmd
Rule Key: pmd:DoubleCheckedLocking


Partially created objects can be returned by the Double Checked Locking pattern when used in Java. An optimizing JRE may assign a reference to the baz variable before it creates the object the reference is intended to point to. More details. Example :

public class Foo {
  Object baz;
  Object bar() {
    if(baz == null) { //baz may be non-null yet not fully created
      synchronized(this){
        if(baz == null){
          baz = new Object();
        }
      }
    }
    return baz;
  }
}