Class ReentrantFileLock
- java.lang.Object
-
- java.util.concurrent.locks.ReentrantLock
-
- org.droolsassert.util.ReentrantFileLock
-
- All Implemented Interfaces:
Serializable,Lock
public class ReentrantFileLock extends ReentrantLock
CombinesReentrantLockwithFileLock
Suitable 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 yourReentrantFileLockshould 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.
ReentrantLockis 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 anyReentrantFileLock(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:
FileLock, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classReentrantFileLock.ReentrantFileLockFactory
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description FilegetAbsoluteFile()voidlock()Unlike other lock methods this one served fairly when enqueued to heavy used file resource.voidlockInterruptibly()static ReentrantFileLock.ReentrantFileLockFactorynewReentrantFileLockFactory(boolean fair, File file)static ReentrantFileLock.ReentrantFileLockFactorynewReentrantFileLockFactory(boolean fair, String filePath)static ReentrantFileLock.ReentrantFileLockFactorynewReentrantFileLockFactory(File file)static ReentrantFileLock.ReentrantFileLockFactorynewReentrantFileLockFactory(String filePath)static ReentrantFileLock.ReentrantFileLockFactorynewReentrantResourceLockFactory(boolean fair, String resourcePath)static ReentrantFileLock.ReentrantFileLockFactorynewReentrantResourceLockFactory(String resourcePath)StringtoString()booleantryLock()booleantryLock(long timeout, TimeUnit unit)voidunlock()-
Methods inherited from class java.util.concurrent.locks.ReentrantLock
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, newCondition
-
-
-
-
Method Detail
-
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
public void lockInterruptibly() throws InterruptedException- Specified by:
lockInterruptiblyin interfaceLock- Overrides:
lockInterruptiblyin classReentrantLock- Throws:
InterruptedException
-
tryLock
public boolean tryLock()
- Specified by:
tryLockin interfaceLock- Overrides:
tryLockin classReentrantLock
-
tryLock
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException- Specified by:
tryLockin interfaceLock- Overrides:
tryLockin classReentrantLock- Throws:
InterruptedException
-
unlock
public void unlock()
- Specified by:
unlockin interfaceLock- Overrides:
unlockin classReentrantLock
-
getAbsoluteFile
public File getAbsoluteFile()
-
toString
public String toString()
- Overrides:
toStringin classReentrantLock
-
-