Menu

#884 False +: DoubleCheckedLocking warning with volatile field

closed
None
5
2012-10-07
2009-07-23
Nick Radov
No

According to this article, double checked locking is safe in Java 5.0 if the field is volatile: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html. However, the following code still generates a DoubleCheckedLocking warning.

public class Foo {
volatile 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;
}

}

Discussion

  • Jan Ruzicka

    Jan Ruzicka - 2009-08-13

    Is there any differentiation between Java versions of the source code?
    Older versions do not allow this safely.
    In the Java 5 and later, the volatile modifier of the member variable is needed.

     
  • Romain PELISSE

    Romain PELISSE - 2009-08-13

    I'll work on this one and the other one on DCL.

     
  • Romain PELISSE

    Romain PELISSE - 2009-08-15

    I reproduce and fixed this issue. From now on, DCL rule will ignore DCL match that return a volatile typed field. The fix will be available in the next 4.2.6 release and the 5.0.x series also.

    Thanks for your report.

     

Log in to post a comment.