public final class ReentrantFileLock extends FileLock
It will prevent a single thread that uses this interface to acquire locks from causing
an OverlappingFileLockException
. Separate threads will not be prevented from taking
overlapping file locks.
All the usual caveats around file locks apply, shared locks and locks for specific ranges are not supported.
Constructor and Description |
---|
ReentrantFileLock(String canonicalPath,
FileLock fileLock)
Constructs a ReentrantFileLock.
|
Modifier and Type | Method and Description |
---|---|
Channel |
acquiredBy() |
static boolean |
isHeldByCurrentThread(File file)
Is there a cached FileLock held by the current thread for the specified file
|
boolean |
isValid() |
static ReentrantFileLock |
lock(File file,
FileChannel fileChannel)
Take an exclusive lock on the entire file, blocks until lock is acquired
|
void |
release()
Releases the lock.
|
static @Nullable ReentrantFileLock |
tryLock(File file,
FileChannel fileChannel)
Try and take an exclusive lock on the entire file, non-blocking
|
public Channel acquiredBy()
Ensures that only the owning thread accesses the delegate FileLock.
acquiredBy
in class FileLock
public boolean isValid()
Ensures that only the owning thread accesses the delegate FileLock.
public void release() throws IOException
release
in class FileLock
IOException
- If an I/O error occurs.@Nullable public static @Nullable ReentrantFileLock tryLock(File file, FileChannel fileChannel) throws IOException
file
- The file to lockfileChannel
- An open to the file
IOException
- If an I/O error occurs.public static ReentrantFileLock lock(File file, FileChannel fileChannel) throws IOException
file
- The file to lockfileChannel
- An open to the file
IOException
- If an I/O error occurs.public static boolean isHeldByCurrentThread(File file)
file
- The file to checkCopyright © 2024. All rights reserved.