title: Using java file locks within single JVM and across multiple JVMs
body: <p>I guess I miss something, but I cannot understand how file locks work in Java. To be more exact - how it is implemented.</p>
  
  <p>It seems I cannot acquire (even cannot attempt acquiring) two or more locks for the same file inside single JVM. First lock will be successfully acquired, all further attempts to acquire more locks will result in OverlapingFileLockException. Nevertheless it works for separate processes. </p>
  
  <p>I want to implement data-storage backed by file-system which is intended to work with multiple concurrent requests (both read and write). I want to use file locks to lock on particular files in the storage.</p>
  
  <p>It seems that I have to introduce one more synchronization (exclusive) on JVM-level and only then sync on files to avoid this exception.</p>
  
  <p>Did anyone do anything like that?</p>
  
  <p>I prepared simple test case to show what my problem is. I use Mac OS X, Java 6.</p>
  
  <pre><code>import junit.framework.*;
  
  import javax.swing.*;
  import java.io.*;
  import java.nio.channels.*;
  
  /**
   * Java file locks test.
   */
  public class FileLocksTest extends TestCase {
      /** File path (on Windows file will be created under the root directory of the current drive). */
      private static final String LOCK_FILE_PATH = "/test-java-file-lock-tmp.bin";
  
      /**
       * @throws Exception If failed.
       */
      public void testWriteLocks() throws Exception {
          final File file = new File(LOCK_FILE_PATH);
  
          file.createNewFile();
  
          RandomAccessFile raf = new RandomAccessFile(file, "rw");
  
          System.out.println("Getting lock...");
  
          FileLock lock = raf.getChannel().lock();
  
          System.out.println("Obtained lock: " + lock);
  
          Thread thread = new Thread(new Runnable() {
              @Override public void run() {
                  try {
                      RandomAccessFile raf = new RandomAccessFile(file, "rw");
  
                      System.out.println("Getting lock (parallel thread)...");
  
                      FileLock lock = raf.getChannel().lock();
  
                      System.out.println("Obtained lock (parallel tread): " + lock);
  
                      lock.release();
                  }
                  catch (Throwable e) {
                      e.printStackTrace();
                  }
              }
          });
  
          thread.start();
  
          JOptionPane.showMessageDialog(null, "Press OK to release lock.");
  
          lock.release();
  
          thread.join();
      }
  
      /**
       * @throws Exception If failed.
       */
      public void testReadLocks() throws Exception {
          final File file = new File(LOCK_FILE_PATH);
  
          file.createNewFile();
  
          RandomAccessFile raf = new RandomAccessFile(file, "r");
  
          System.out.println("Getting lock...");
  
          FileLock lock = raf.getChannel().lock(0, Long.MAX_VALUE, true);
  
          System.out.println("Obtained lock: " + lock);
  
          Thread thread = new Thread(new Runnable() {
              @Override public void run() {
                  try {
                      RandomAccessFile raf = new RandomAccessFile(file, "r");
  
                      System.out.println("Getting lock (parallel thread)...");
  
                      FileLock lock = raf.getChannel().lock(0, Long.MAX_VALUE, true);
  
                      System.out.println("Obtained lock (parallel thread): " + lock);
  
                      lock.release();
  
                  }
                  catch (Throwable e) {
                      e.printStackTrace();
                  }
              }
          });
  
          thread.start();
  
          JOptionPane.showMessageDialog(null, "Press OK to release lock.");
  
          lock.release();
  
          thread.join();
      }
  }
  </code></pre>