Short version: Why is File.createNewFile() not used to lock files? Or more specifically: are there problems if they are used to lock the application data directory?
Details:
I would like to protect the application data directory with a lock file: if the lock file exists, the directory is locked and the application exits with an error message. If it does not exist, it will be created and the application will continue. Upon exit, the file will be deleted.
A lock will not be created often (i.e., performance is not a problem), and I have no problem manually deleting the lock file in case of some error (i.e., I cannot delete the file, this is not a problem).
The code looks something like this:
File lockFile = new File("lock"); boolean lockCreated = lockFile.createNewFile(); if (lockCreated) { // do stuff lockFile.delete(); } else { System.err.println("Lockfile exists => please retry later"); // alternative: Wait and retry eg 5 times }
Now I'm a little confused in Javadoc createNewFile() :
Atomically creates a new empty file, called this abstract empty file, if and only if the file with this name does not exist yet. Checking for a file and creating a file if it does not exist is one operation that is atomic with respect to all other file system actions that can affect the file.
Note: this method should not be used to lock files, because the resulting protocol cannot be reliably executed. Instead, use FileLock .
What are the potential problems mentioned in the note, given that existence checking and file creation are atomic?
This forum post from December 2007 indicates that there are “significant platform differences” according to Javadoc File.delete () (although I cannot find such an operator with at least Java SE 1.4.2). But even if there were such differences: could they really cause the lock to fail (i.e., two processes believe that the data directory can be used simultaneously)?
Note: I do not want any of the following:
- Lock the file so that no other process can access and / or modify it (most of the data found seem to discuss this problem).
- Verify that another process cannot remove the lock.
- Synchronize multiple threads with the same JVM (although I think my solution can handle this too).
java file locking
siegi
source share