[concurrency-interest] Double Checked Locking in OpenJDK

Stanimir Simeonoff stanimir at riflexo.com
Sun Aug 12 15:53:31 EDT 2012


Hi!
The code looks fine with regard to double-check idiom - filePath is
declared volatile and there is a monitor on write/double check.
String path is final, so it'd guaranteed  to be properly initialized. The
local var. result helps reduce the volatile reads.

No bug in that code (setLastModified javadocs is still incorrect for FAT
file system, only even seconds allowed there).


Stanimir

On Sun, Aug 12, 2012 at 10:35 PM, Richard Warburton <
richard.warburton at gmail.com> wrote:

> Hello,
>
> 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?
>
> regards,
>
>   Richard
>
> [0] See the end of
>
> hg.openjdk.java.net/jdk8/jdk8/jdk/file/da8649489aff/src/share/classes/java/io/File.java
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120812/d6e5682c/attachment-0001.html>


More information about the Concurrency-interest mailing list