NIO locking the file



  • In the example, I'm setting up two flows that are trying to get elbows on the same file, and I'll give you an example of one flow. th2 and starts immediately after the 1st:

                Thread th1 = new Thread(new Runnable() {
                Object obj = new Object();
                @Override
                public void run() {
                    synchronized (obj) {
                        FileLock lock = null;
                        FileChannel fileChannel = null;
                        try {
                            fileChannel = FileChannel.open(new File("sample.txt").toPath(), StandardOpenOption.READ);
                            lock = fileChannel.tryLock(0, 2, true);
                            System.out.println("1acq by " + lock.acquiredBy());
                            System.out.println("1Lock acquired: " + lock.isValid());
                            System.out.println("1Lock is shared: " + lock.isShared());
                            System.out.println(" thread 1");
                            obj.wait(2000);
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fileChannel.close();
                            lock.release();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    
        th1.start();
    

    But in fact, some of the second flow doesn't make it easy to get the elbow:

    java.nio.channels.OverlappingFileLockException

    What says in the documentation that a shared lock on the reading can be taken at the same time!

    PS file exists and its size is large enough

    At the same time, the PPS was able to obtain the elbow only through different processes! Why?



  • In the documentation, it is written that a shared lock on the reading channel is possible. Take a few!

    That's right. One amendment: File locking is available only different processes. I mean, trying to get the elbows on a file from different streams of one process is incorrect. If you want to guarantee that only one flower has access to the file-- use the usual location on the site. From http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#tryLock(long,%20long,%20boolean) :

    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.

    shared lock simply means that another process will not be able to take exclusive lock, but only shared. From http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileLock.html :

    A shared lock prevents other concurrently-running programs from acquiring an overlapping exclusive lock, but does allow them to acquire overlapping shared locks.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2