Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java File Locking

Tags:

java

locking

I have several threads (some of which are spawned by Process X, others by Process Y, et cetera), and each thread needs to write to a file MyFile. However, if Thread T1 starts writing to MyFile first, then, when Thread T2 starts writing, it needs to wait for T1 to release the file, so that it can read the contents that were written in Thread T1. In other words, each thread would have a finalizeThread method, like so:

private void finalizeThread() {
    File f = new File("MyFile.dat");
    f.createNewFile();  // atomically creates the file, if it doesn't exist
    locked_section {
        readContentsFromFile(f); // read contents if some other thread already modified the file
        modifyContentsFromFile(f); // modify
        writeFile(f); // write, so that new threads can see the content modified by this thread
    }
}

My question is: How can I accomplish the locked_section in the above code? I was looking into the FileLock class, but it says in the Javadoc that "File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.".

like image 328
João Silva Avatar asked Jan 11 '10 23:01

João Silva


People also ask

What is file locking in Java?

A token representing a lock on a region of a file. A file-lock object is created each time a lock is acquired on a file via one of the lock or tryLock methods of the FileChannel class, or the lock or tryLock methods of the AsynchronousFileChannel class. A file-lock object is initially valid.

What are some of the methods that Java provides to lock files?

In Java, a file lock can be obtained using FileChannel , which provides two methods — lock() and tryLock() — for this purpose. The lock() method acquires an exclusive lock on entire file, whereas the lock(long position, long size, boolean shared) method can be used to acquire a lock on the given region of a ile.


2 Answers

If the file is only accessed from your program, the synchronized lock object is okay. But if you want to protect the file from being changed by other programs while you're working on it, you can use Java's file locking features in java.nio.channels.FileLock (example). As the text says, mind that on some operating systems, programs can still change files if they don't check for an existing file lock.

like image 181
AndiDog Avatar answered Oct 21 '22 04:10

AndiDog


Instead of sharing a lock, maybe you could have a separate process that was responsible for maintaining a lock on the file. To begin your read/modify/write step, a Thread would have to ask this central process for the lock via HTTP, or messaging, or whatever you like. If the request is denied, the Thread would go to sleep, wake up, and try again. Otherwise the Thread would read/modify/write and then tell the locking process that it is releasing the lock.

like image 2
danben Avatar answered Oct 21 '22 04:10

danben