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.logger
Constructor and Description |
---|
FileLockNamedLock(String name,
FileChannel fileChannel,
NamedLockFactorySupport factory) |
Modifier and Type | Method and Description |
---|---|
boolean |
lockExclusively(long time,
TimeUnit unit)
Tries to lock exclusively, may block for given time.
|
boolean |
lockShared(long time,
TimeUnit unit)
Tries to lock shared, may block for given time.
|
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
public FileLockNamedLock(String name, FileChannel fileChannel, NamedLockFactorySupport factory)
public boolean lockShared(long time, TimeUnit unit) throws InterruptedException
NamedLock
true
.InterruptedException
public boolean lockExclusively(long time, TimeUnit unit) throws InterruptedException
NamedLock
true
.InterruptedException
public void unlock()
NamedLock
NamedLock.lockShared(long, TimeUnit)
or NamedLock.lockExclusively(long, TimeUnit)
.Copyright © 2010–2022 The Apache Software Foundation. All rights reserved.