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) |
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() |
static @Nullable ReentrantFileLock |
tryLock(File file,
FileChannel fileChannel)
Try and take an exclusive lock on the entire file, non-blocking
|
public Channel acquiredBy()
acquiredBy
in class FileLock
public void release() throws IOException
release
in class FileLock
IOException
@Nullable public static @Nullable ReentrantFileLock tryLock(File file, FileChannel fileChannel) throws IOException
file
- The file to lockfileChannel
- An open to the file
IOException
public static ReentrantFileLock lock(File file, FileChannel fileChannel) throws IOException
file
- The file to lockfileChannel
- An open to the file
IOException
public static boolean isHeldByCurrentThread(File file)
file
- The file to checkCopyright © 2023. All rights reserved.