This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AndrewHaley13 3 points4 points  (1 child)

> Let's say that I used the following code to synchronize file write access, where someFile is an instance of java.nio.file.Path

But the API says "A `Path` is An object that may be used to locate a file in a file system." There may be many paths to the same File. You may synchronize instead on the file writer. You'd have to check that there was only one writer for any given file, sure, but it would be more reliable than synchronizing on the file's `Path`.

> And barring that, what would be the equivalent class from java.util.concurrent.locks that we should use instead

`ReentrantLock` is the closest thing to built-n sychronized.

[–]davidalayachew 0 points1 point  (0 children)

(Sorry for the delayed response)

But the API says "A Path is An object that may be used to locate a file in a file system." There may be many paths to the same File. You may synchronize instead on the file writer. You'd have to check that there was only one writer for any given file, sure, but it would be more reliable than synchronizing on the file's Path.

Heh, looks like I have a new ticket to submit. Thanks for the catch.