public interface DistributedLock extends Lock
Distributed lock provides support for controlling access to a shared resource or a critical section at the cluster-wide level. At any
point in time only one thread on any cluster node can gain a lock with the same name
. All other threads, either running
on the same node or on remote nodes, will await for the lock to be released before getting a chance to obtain the lock.
Below is the example of how an instance of DistributedLock
can be obtained from the LockService
and used for locking
operations:
// Get lock with name 'example.lock'.
DistributedLock lock = hekate.locks().region("region1").get("example.lock");
// Acquire the lock.
lock.lock();
try {
// Do some work ...
thereCanBeOnlyOne();
} finally {
// Make sure that lock is always released after the work is done.
lock.unlock();
}
For more details about distributed locks and their usage please see the documentation of LockService
interface.
LockService
Modifier and Type | Method and Description |
---|---|
int |
holdCount()
Returns the number of holds on this lock by the current thread.
|
boolean |
isHeldByCurrentThread()
Returns
true if this lock is held by the current thread. |
Future<?> |
lockAsync(Executor executor,
AsyncLockCallback callback)
Performs asynchronous locking and notifies the specified callback upon the lock status changes.
|
String |
name()
Returns the name of this lock.
|
Condition |
newCondition()
Unsupported operation.
|
Optional<LockOwnerInfo> |
owner()
Returns an information about the node that is currently holding this lock.
|
String |
regionName()
Returns the name of the region this lock belongs to.
|
boolean |
tryLock(long timeout,
TimeUnit unit)
Tries to acquire the lock with the given timeout.
|
void |
unlock()
Unlocks the lock.
|
void |
unlockAsync()
Schedules unlock operation for asynchronous processing but doesn't await for its completion.
|
lock, lockInterruptibly, tryLock
String name()
LockRegion.get(String)
String regionName()
LockService.region(String)
int holdCount()
boolean isHeldByCurrentThread()
true
if this lock is held by the current thread.true
if this lock is held by the current thread.Future<?> lockAsync(Executor executor, AsyncLockCallback callback)
The specified Executor
instance will be used to perform all notifications of the callback object. Thus it is highly
recommended to use a single-threaded executor (f.e. Executors.newSingleThreadExecutor()
) so that all the callback
notifications would be performed on the same thread. Otherwise the callback notification order can be completely unpredictable.
For details about the callback notification order please see the documentation of AsyncLockCallback
interface.
The Future
object, that is returned by this method, can be used to wait for asynchronous lock acquisition (after the lock
have been acquired but before the AsyncLockCallback.onLockAcquire(DistributedLock)
method gets notified).
executor
- Executor to perform asynchronous notifications of callback methods.callback
- Callback.void unlock()
IllegalMonitorStateException
will be thrown.void unlockAsync()
IllegalMonitorStateException
will be thrown.boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException
Note: timeout doesn't consider communication overhead of locking. For example, if timeout is 100ms and it takes 50ms to communicate with the cluster members in order to try the lock then the total wait time before giving up will be 150ms.
tryLock
in interface Lock
timeout
- Maximum time to wait for the lock (doesn't include the communication overhead).unit
- Time unit of timeout.true
if the lock was acquired and false
if the waiting time elapsed before the lock was acquired.InterruptedException
- Signals that current thread is interrupted while acquiring the lock.Optional<LockOwnerInfo> owner() throws InterruptedException
Note that this operation requires a network round trip to the lock manager node and there are no guarantees that lock owner will not change by the time when result is returned from this method.
InterruptedException
- Signal that current thread was interrupted while awaiting for lock owner information.Condition newCondition()
newCondition
in interface Lock