public final class FileLockNamedLock extends NamedLockSupport
FileLock
. An instance of this class is about ONE LOCK (one file)
and is possibly used by multiple threads. Each thread (if properly coded re boxing) will try to
obtain either shared or exclusive lock. As file locks are JVM-scoped (so one JVM can obtain
same file lock only once), the threads share file lock and synchronize according to it. Still,
as file lock obtain operation does not block (or in other words, the method that does block
cannot be controlled for how long it blocks), we are "simulating" thread blocking using
Retry
utility.
This implementation performs coordination not only on thread (JVM-local) level, but also on
process level, as long as other parties are using this same "advisory" locking mechanism.Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.locks.ReentrantLock |
criticalRegion
Lock protecting "critical region": this is where threads are allowed to perform locking but should leave this
region as quick as possible.
|
private java.nio.channels.FileChannel |
fileChannel
The
FileChannel this instance is about. |
private java.util.concurrent.atomic.AtomicReference<java.nio.channels.FileLock> |
fileLockRef
The reference of
FileLock , if obtained. |
private static long |
LOCK_POSITION |
private static long |
LOCK_SIZE |
private static long |
RETRY_SLEEP_MILLIS |
private java.util.Map<java.lang.Thread,java.util.Deque<java.lang.Boolean>> |
threadSteps
Thread -> steps stack (true = shared, false = exclusive)
|
logger
Constructor and Description |
---|
FileLockNamedLock(java.lang.String name,
java.nio.channels.FileChannel fileChannel,
NamedLockFactorySupport factory) |
Modifier and Type | Method and Description |
---|---|
private boolean |
anyOtherThreadHasSteps()
Returns
true if any other than this thread using this instance has any step recorded. |
private java.lang.Boolean |
doLockExclusively() |
private java.lang.Boolean |
doLockShared() |
boolean |
lockExclusively(long time,
java.util.concurrent.TimeUnit unit)
Tries to lock exclusively, may block for given time.
|
boolean |
lockShared(long time,
java.util.concurrent.TimeUnit unit)
Tries to lock shared, may block for given time.
|
private java.nio.channels.FileLock |
obtainFileLock(boolean shared)
Attempts to obtain real
FileLock , returns non-null value is succeeds, or null if cannot. |
void |
unlock()
Unlocks the lock, must be invoked by caller after one of the
NamedLock.lockShared(long, TimeUnit) or NamedLock.lockExclusively(long, TimeUnit) . |
close, name
private static final long RETRY_SLEEP_MILLIS
private static final long LOCK_POSITION
private static final long LOCK_SIZE
private final java.util.Map<java.lang.Thread,java.util.Deque<java.lang.Boolean>> threadSteps
private final java.nio.channels.FileChannel fileChannel
FileChannel
this instance is about.private final java.util.concurrent.atomic.AtomicReference<java.nio.channels.FileLock> fileLockRef
FileLock
, if obtained.private final java.util.concurrent.locks.ReentrantLock criticalRegion
public FileLockNamedLock(java.lang.String name, java.nio.channels.FileChannel fileChannel, NamedLockFactorySupport factory)
public boolean lockShared(long time, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
NamedLock
true
.java.lang.InterruptedException
public boolean lockExclusively(long time, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
NamedLock
true
.java.lang.InterruptedException
private java.lang.Boolean doLockShared()
private java.lang.Boolean doLockExclusively()
public void unlock()
NamedLock
NamedLock.lockShared(long, TimeUnit)
or NamedLock.lockExclusively(long, TimeUnit)
.private boolean anyOtherThreadHasSteps()
true
if any other than this thread using this instance has any step recorded.private java.nio.channels.FileLock obtainFileLock(boolean shared)
FileLock
, returns non-null value is succeeds, or null
if cannot.