public class ReentrantFileLock extends ReentrantLock
ReentrantLock with FileLockReentrantFileLock should be static to have single JVM instance of the lock object correspond to file lock for the entire JVM.
Inner logic is fairly simple and imply acquiring first java ReentrantLock (fairness option belong here) and, when succeeded, continue further with acquiring file
lock.
Release logic behaves in the opposite order.
ReentrantLock is reentrant, meaning the same thread can re-acquire the same lock again (lock held count get incremented).
The same logic implemented for the file lock, meaning any ReentrantFileLock(s) can re-acquire the same lock id on the file running within the same JVM (file lock held
count get incremented implying no interaction with file system).
You can also synchronize on resources which are files on file system, like configuration files etc. Files will be locked for write though.
Consider Initialization-on-demand holder idiom for lazy loading
private static final ReentrantFileLockFactory fileLockFactory = newReentrantFileLockFactory("target/droolsassert/lock");
private static final ReentrantFileLock consolidatedReportLock = fileLockFactory.newLock(ActivationReportBuilder.class.getName());
consolidatedReportLock.lock();
try {
...
} finally {
consolidatedReportLock.unlock();
}
FileLock,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
static class |
ReentrantFileLock.ReentrantFileLockFactory |
| Modifier and Type | Method and Description |
|---|---|
File |
getAbsoluteFile() |
void |
lock()
Unlike other lock methods this one served fairly when enqueued to heavy used file resource.
|
void |
lockInterruptibly() |
static ReentrantFileLock.ReentrantFileLockFactory |
newReentrantFileLockFactory(boolean fair,
File file) |
static ReentrantFileLock.ReentrantFileLockFactory |
newReentrantFileLockFactory(boolean fair,
String filePath) |
static ReentrantFileLock.ReentrantFileLockFactory |
newReentrantFileLockFactory(File file) |
static ReentrantFileLock.ReentrantFileLockFactory |
newReentrantFileLockFactory(String filePath) |
static ReentrantFileLock.ReentrantFileLockFactory |
newReentrantResourceLockFactory(boolean fair,
String resourcePath) |
static ReentrantFileLock.ReentrantFileLockFactory |
newReentrantResourceLockFactory(String resourcePath) |
String |
toString() |
boolean |
tryLock() |
boolean |
tryLock(long timeout,
TimeUnit unit) |
void |
unlock() |
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, newConditionpublic static final ReentrantFileLock.ReentrantFileLockFactory newReentrantFileLockFactory(String filePath)
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantFileLockFactory(boolean fair, String filePath)
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantFileLockFactory(File file)
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantFileLockFactory(boolean fair, File file)
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantResourceLockFactory(String resourcePath)
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantResourceLockFactory(boolean fair, String resourcePath)
public void lock()
lock in interface Locklock in class ReentrantLockpublic void lockInterruptibly()
throws InterruptedException
lockInterruptibly in interface LocklockInterruptibly in class ReentrantLockInterruptedExceptionpublic boolean tryLock()
tryLock in interface LocktryLock in class ReentrantLockpublic boolean tryLock(long timeout,
TimeUnit unit)
throws InterruptedException
tryLock in interface LocktryLock in class ReentrantLockInterruptedExceptionpublic void unlock()
unlock in interface Lockunlock in class ReentrantLockpublic File getAbsoluteFile()
public String toString()
toString in class ReentrantLockCopyright © 2021. All rights reserved.