Interface FileLock
- All Superinterfaces:
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 TypeMethodDescriptionReturns the channel through which this lock was acquired.voidclose()Releases this lock.booleanisShared()Returns whether this lock is shared.booleanisValid()Returns whether this lock is valid.longposition()Returns the position of the first byte in the locked region.longsize()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
-
isValid
boolean isValid()Returns whether this lock is valid.- Returns:
trueif this lock is valid- Since:
- 25.5
-
close
Releases this lock. If the lock is already invalid, invoking this method has no effect.- Specified by:
closein interfaceAutoCloseable- Throws:
ClosedChannelException- if the channel used to acquire this lock is closedIOException- if an I/O error occurs- Since:
- 25.5
-