public interface ILock extends Lock, DistributedObject
Lock
.
Lock mylock = Hazelcast.getLock(mylockobject); mylock.lock();
try {
// do something
} finally {
mylock.unlock();
}
//Lock on Map
IMap map = Hazelcast.getMap("customers"); map.lock("1");
try {
// do something
} finally {
map.unlock("1"); }
Behaviour of ILock
under split-brain scenarios should be taken into account when using this
data structure. During a split, each partitioned cluster will either create a brand new and un-acquired
ILock
or it will continue to use the primary or back-up version. As the acquirer of the ILock
might
reside in a different partitioned network this can lead to situations where the lock is never obtainable.
When the split heals, Hazelcast performs a default largest cluster wins resolution. Where the clusters are
the same size a winner of the merge will be randomly chosen. In any case, this can lead to situations where
(post-merge) multiple acquirers think they hold the same lock, when in fact the ILock
itself records only one
owner. When the false owners come to release the ILock
an IllegalMonitorStateException
is
thrown.
Acquiring an ILock
with a lease time lock(long, TimeUnit)
can help to mitigate such scenarios.
As a defensive mechanism against such inconsistency, consider using the in-built split-brain protection for lock. Using this functionality it is possible to restrict operations in smaller partitioned clusters. It should be noted that there is still an inconsistency window between the time of the split and the actual detection. Therefore using this reduces the window of inconsistency but can never completely eliminate it.
Lock
Modifier and Type | Method and Description |
---|---|
void |
forceUnlock()
Releases the lock regardless of the lock owner.
|
Object |
getKey()
Deprecated.
use
DistributedObject.getName() instead. |
int |
getLockCount()
Returns re-entrant lock hold count, regardless of lock ownership.
|
long |
getRemainingLeaseTime()
Returns remaining lease time in milliseconds.
|
boolean |
isLocked()
Returns whether this lock is locked or not.
|
boolean |
isLockedByCurrentThread()
Returns whether this lock is locked by current thread or not.
|
void |
lock() |
void |
lock(long leaseTime,
TimeUnit timeUnit)
Acquires the lock for the specified lease time.
|
Condition |
newCondition()
This method is not implemented! Use
newCondition(String) instead. |
ICondition |
newCondition(String name)
Returns a new
ICondition instance that is bound to this
ILock instance with given name. |
boolean |
tryLock() |
boolean |
tryLock(long time,
TimeUnit unit) |
boolean |
tryLock(long time,
TimeUnit unit,
long leaseTime,
TimeUnit leaseUnit)
Tries to acquire the lock for the specified lease time.
|
void |
unlock()
Releases the lock.
|
lockInterruptibly
destroy, getName, getPartitionKey, getServiceName
@Deprecated Object getKey()
DistributedObject.getName()
instead.boolean tryLock(long time, TimeUnit unit) throws InterruptedException
tryLock
in interface Lock
InterruptedException
boolean tryLock(long time, TimeUnit unit, long leaseTime, TimeUnit leaseUnit) throws InterruptedException
After lease time, the lock will be released.
If the lock is not available, then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
time
- maximum time to wait for the lock.unit
- time unit of the time argument.leaseTime
- time to wait before releasing the lock.leaseUnit
- unit of time to specify lease time.InterruptedException
void lock(long leaseTime, TimeUnit timeUnit)
After lease time, lock will be released..
If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.
leaseTime
- time to wait before releasing the lock.timeUnit
- unit of time for the lease time.IllegalMonitorStateException
- if the current thread does not hold this lockvoid forceUnlock()
Condition newCondition()
newCondition(String)
instead.newCondition
in interface Lock
UnsupportedOperationException
- the exception is always thrown, since this method is not implementedICondition newCondition(String name)
ICondition
instance that is bound to this
ILock
instance with given name.
Before waiting on the condition the lock must be held by the
current thread.
A call to Condition.await()
will atomically release the lock
before waiting and re-acquire the lock before the wait returns.
name
- identifier of the new condition instanceICondition
instance for this ILock
instanceNullPointerException
- if name is null.boolean isLocked()
true
if this lock is locked, false
otherwise.boolean isLockedByCurrentThread()
true
if this lock is locked by current thread, false
otherwise.int getLockCount()
long getRemainingLeaseTime()
Copyright © 2018 Hazelcast, Inc.. All Rights Reserved.