Interface FileLock

All Superinterfaces:
AutoCloseable

public interface FileLock extends AutoCloseable
Represents a lock on a region of a file acquired through a FileChannel.

A lock remains valid until it is released by close() or its associated channel is closed, whichever occurs first. File locks are held on behalf of the entire Java virtual machine and are not suitable for coordinating access between threads in the same virtual machine.

Since:
25.5
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the channel through which this lock was acquired.
    void
    Releases this lock.
    boolean
    Returns whether this lock is shared.
    boolean
    Returns whether this lock is valid.
    long
    Returns the position of the first byte in the locked region.
    long
    Returns the size of the locked region in bytes.
  • Method Details

    • acquiredBy

      FileChannel acquiredBy()
      Returns the channel through which this lock was acquired.
      Returns:
      the channel through which this lock was acquired
      Since:
      25.5
    • position

      long position()
      Returns the position of the first byte in the locked region. The position may exceed the current size of the file.
      Returns:
      the position of the first byte in the locked region
      Since:
      25.5
    • size

      long size()
      Returns the size of the locked region in bytes. The locked region need not be contained within or overlap the current file contents.
      Returns:
      the size of the locked region in bytes
      Since:
      25.5
    • isShared

      boolean isShared()
      Returns whether this lock is shared. A request for a shared lock may be converted to an exclusive lock when shared locks are not supported by the operating system.
      Returns:
      true if this lock is shared, or false if it is exclusive
      Since:
      25.5
    • isValid

      boolean isValid()
      Returns whether this lock is valid.
      Returns:
      true if this lock is valid
      Since:
      25.5
    • close

      void close() throws IOException
      Releases this lock. If the lock is already invalid, invoking this method has no effect.
      Specified by:
      close in interface AutoCloseable
      Throws:
      ClosedChannelException - if the channel used to acquire this lock is closed
      IOException - if an I/O error occurs
      Since:
      25.5