Class ReentrantFileLock
- All Implemented Interfaces:
Serializable,Lock
ReentrantLock with FileLockSuitable to synchronize threads from different VMs via file system.
As per documentation, file locks are held on behalf of the entire Java virtual machine.
Thus in most cases your
ReentrantFileLock should be static to have single JVM instance of the lock object correspond to file lock for the entire JVM.But it should not be an error to have several exclusive locks on JVM level correspond to single file lock id when you have valid technical usecase to do so.
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();
}
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidlock()Unlike other lock methods this one served fairly when enqueued to heavy used file resource.voidstatic final ReentrantFileLock.ReentrantFileLockFactorynewReentrantFileLockFactory(boolean fair, File file) static final ReentrantFileLock.ReentrantFileLockFactorynewReentrantFileLockFactory(boolean fair, String filePath) static final ReentrantFileLock.ReentrantFileLockFactorystatic final ReentrantFileLock.ReentrantFileLockFactorynewReentrantFileLockFactory(String filePath) static final ReentrantFileLock.ReentrantFileLockFactorynewReentrantResourceLockFactory(boolean fair, String resourcePath) static final ReentrantFileLock.ReentrantFileLockFactorynewReentrantResourceLockFactory(String resourcePath) toString()booleantryLock()booleanvoidunlock()Methods inherited from class java.util.concurrent.locks.ReentrantLock
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, newCondition
-
Method Details
-
newReentrantFileLockFactory
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantFileLockFactory(String filePath) -
newReentrantFileLockFactory
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantFileLockFactory(boolean fair, String filePath) -
newReentrantFileLockFactory
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantFileLockFactory(File file) -
newReentrantFileLockFactory
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantFileLockFactory(boolean fair, File file) -
newReentrantResourceLockFactory
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantResourceLockFactory(String resourcePath) -
newReentrantResourceLockFactory
public static final ReentrantFileLock.ReentrantFileLockFactory newReentrantResourceLockFactory(boolean fair, String resourcePath) -
lock
public void lock()Unlike other lock methods this one served fairly when enqueued to heavy used file resource.- Specified by:
lockin interfaceLock- Overrides:
lockin classReentrantLock
-
lockInterruptibly
- Specified by:
lockInterruptiblyin interfaceLock- Overrides:
lockInterruptiblyin classReentrantLock- Throws:
InterruptedException
-
tryLock
public boolean tryLock()- Specified by:
tryLockin interfaceLock- Overrides:
tryLockin classReentrantLock
-
tryLock
- Specified by:
tryLockin interfaceLock- Overrides:
tryLockin classReentrantLock- Throws:
InterruptedException
-
unlock
public void unlock()- Specified by:
unlockin interfaceLock- Overrides:
unlockin classReentrantLock
-
getAbsoluteFile
-
toString
- Overrides:
toStringin classReentrantLock
-