[concurrency-interest] Double Checked Locking in OpenJDK

Richard Warburton richard.warburton at gmail.com
Sun Aug 12 15:35:51 EDT 2012


The current implementation of java.io.File::toPath [0] appears to be
using the double checked locking pattern:

     public Path toPath() {
         Path result = filePath;
         if (result == null) {
             synchronized (this) {
                 result = filePath;
                 if (result == null) {
                     result = FileSystems.getDefault().getPath(path);
                     filePath = result;
         return result;

I was going to report the bug, but I'm a little uncertain of the
interaction between the local variable 'result' and DCL since I've
previously only seen the checking condition on the shared field
itself.  Can someone here either confirm that its a bug or explain how
the 'result' variable is fixing things?



[0] See the end of

More information about the Concurrency-interest mailing list