public interface IgniteLock extends Lock, Closeable
java.util.concurrent.ReentrantLock
.
Ignite.reentrantLock(String, boolean, boolean, boolean)
.
IgniteException
will be thrown on every other node attempting to
perform any operation on this lock. No automatic recovery will be attempted,
and lock will be marked as broken (i.e. unusable), which can be checked using the method #isBroken().
Broken lock cannot be reused again.
java.util.concurrent.ReentrantLock
).
Thus, one of many threads on any node using a fair lock may obtain it multiple times in succession while other
active threads are not progressing and not currently holding the lock. Also note that the untimed tryLock method
does not honor the fairness setting. It will succeed if the lock is available even if other threads are waiting.
As a rule of thumb, whenever there is a reasonable time window between successive calls to release and acquire
the lock, non-fair lock should be preferred:
while(someCondition){
// do anything
lock.lock();
try{
// ...
}
finally {
lock.unlock();
}
}
If successive calls to release/acquire are following immediately,
e.g.
while(someCondition){
lock.lock();
try {
// do something
}
finally {
lock.unlock();
}
}
using the fair lock is reasonable in order to allow even distribution of load among nodes
(although overall throughput may be lower due to increased overhead).Modifier and Type | Method and Description |
---|---|
void |
close()
Removes reentrant lock.
|
int |
getHoldCount()
Queries the number of holds on this lock by the current thread.
|
IgniteCondition |
getOrCreateCondition(String name)
Returns a
Condition instance for use with this
IgniteLock instance. |
int |
getWaitQueueLength(IgniteCondition condition)
Returns an estimate of the number of threads on this node that are waiting on the
given condition associated with this lock.
|
boolean |
hasQueuedThread(Thread thread)
Queries whether the given thread is waiting to acquire this
lock.
|
boolean |
hasQueuedThreads()
Queries whether any threads on this node are waiting to acquire this lock.
|
boolean |
hasWaiters(IgniteCondition condition)
Queries whether any threads on this node are waiting on the given condition
associated with this lock.
|
boolean |
isBroken()
Returns true if any node that owned the locked failed before releasing the lock.
|
boolean |
isFailoverSafe()
Returns
true if this lock is safe to use after node failure. |
boolean |
isFair()
Returns
true if this lock is fair. |
boolean |
isHeldByCurrentThread()
Queries if this lock is held by the current thread.
|
boolean |
isLocked()
Queries if this lock is held by any thread on any node.
|
void |
lock()
Acquires the distributed reentrant lock.
|
void |
lockInterruptibly()
Acquires the lock unless the current thread is
interrupted.
|
String |
name()
Name of atomic reentrant lock.
|
Condition |
newCondition()
This method is not supported in IgniteLock,
Any invocation of this method will result in UnsupportedOperationException.
|
boolean |
removed()
Gets status of reentrant lock.
|
boolean |
tryLock()
Acquires the lock only if it is free at the time of invocation.
|
boolean |
tryLock(long timeout,
TimeUnit unit)
Acquires the lock if it is not held by another thread within the given
waiting time and the current thread has not been
interrupted.
|
void |
unlock()
Releases the lock.
|
String name()
void lock() throws IgniteException
Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
lock
in interface Lock
IgniteException
- if the node is stopped or broken in non-failoverSafe modevoid lockInterruptibly() throws IgniteInterruptedException, IgniteException
Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
If the lock is acquired by the current thread then the lock hold count is set to one.
If the current thread:
IgniteInterruptedException
is thrown and the current thread's
interrupted status is cleared.
IgniteException
is thrown in case:
In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock.
lockInterruptibly
in interface Lock
IgniteInterruptedException
- if the current thread is interruptedIgniteException
- if the lock is broken in non-failoverSafe mode (any node failed while owning this lock),
or local node is stoppedboolean tryLock() throws IgniteException
Acquires the lock if it is available and returns immediately
with the value true
.
If the lock is not available then this method will return
immediately with the value false
.
A typical usage idiom for this method would be:
Lock lock = ...;
if (lock.tryLock()) {
try {
// manipulate protected state
} finally {
lock.unlock();
}
} else {
// perform alternative actions
}
This usage ensures that the lock is unlocked if it was acquired, and
doesn't try to unlock if the lock was not acquired.
If node is stopped, or any node failed while owning the lock in non-failoverSafe mode,
then IgniteException
is thrown.tryLock
in interface Lock
true
if the lock was acquired and
false
otherwiseIgniteException
- if node is stopped, or lock is already broken in non-failover safe modeboolean tryLock(long timeout, TimeUnit unit) throws IgniteInterruptedException, IgniteException
Acquires the lock if it is not held by another thread and returns
immediately with the value true
, setting the lock hold count
to one.
If the current thread
already holds this lock then the hold count is incremented by one and
the method returns true
.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of five things happens:
If the lock is acquired then the value true
is returned and
the lock hold count is set to one.
If the current thread:
IgniteInterruptedException
is thrown and the current thread's
interrupted status is cleared.
IgniteException
is thrown in case:
If the specified waiting time elapses then the value false
is returned. If the time is less than or equal to zero, the method
will not wait at all.
In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock, and over reporting the elapse of the waiting time.
tryLock
in interface Lock
timeout
- the time to wait for the lockunit
- the time unit of the timeout argumenttrue
if the lock was free and was acquired by the
current thread, or the lock was already held by the current
thread; and false
if the waiting time elapsed before
the lock could be acquiredIgniteInterruptedException
- if the current thread is interruptedIgniteException
- if node is stopped, or lock is already broken in non-failover safe modeNullPointerException
- if the time unit is nullvoid unlock() throws IgniteInterruptedException
IllegalMonitorStateException
is thrown.
If lock is already broken prior to invocation of this method, and
lock is created in non-failover safe mode, then IgniteException
is thrown.unlock
in interface Lock
IllegalMonitorStateException
- if not owned by current threadIgniteException
- if node is stopped, or lock is already broken in non-failover safe modeIgniteInterruptedException
IgniteCondition getOrCreateCondition(String name) throws IgniteException
Condition
instance for use with this
IgniteLock
instance.
Condition
waiting or signalling methods are called, then an IllegalMonitorStateException
is thrown.
IgniteInterruptedException
will be thrown, and the thread's
interrupted status will be cleared.
name
- Name of the distributed condition objectIgniteException
- if the lock is not initialized or already removedCondition newCondition()
newCondition
in interface Lock
int getHoldCount() throws IgniteException
IgniteException
- if the lock is not initialized or already removedboolean isHeldByCurrentThread() throws IgniteException
true
if current thread holds this lock and
false
otherwiseIgniteException
- if the lock is not initialized or already removedboolean isLocked() throws IgniteException
true
if any thread on this or any other node holds this lock and
false
otherwiseIgniteException
- if the lock is not initialized or already removedboolean hasQueuedThreads() throws IgniteException
true
return does not guarantee that any other thread will ever
acquire this lock. This method is designed primarily for use in
monitoring of the system state.true
if there may be other threads on this node waiting to
acquire the lockIgniteException
- if the lock is not initialized or already removedboolean hasQueuedThread(Thread thread) throws IgniteException
true
return does not guarantee that this thread
will ever acquire this lock. This method is designed primarily for use
in monitoring of the system state.thread
- the threadtrue
if the given thread is queued waiting for this lockNullPointerException
- if the thread is nullIgniteException
- if the lock is not initialized or already removedboolean hasWaiters(IgniteCondition condition) throws IgniteException
true
return does
not guarantee that a future signal
will awaken any
threads. This method is designed primarily for use in
monitoring of the system state.condition
- the conditiontrue
if there are any waiting threads on this nodeIllegalMonitorStateException
- if this lock is not heldIllegalArgumentException
- if the given condition is
not associated with this lockNullPointerException
- if the condition is nullIgniteException
- if the lock is not initialized or already removedint getWaitQueueLength(IgniteCondition condition) throws IgniteException
condition
- the conditionIllegalMonitorStateException
- if this lock is not heldIllegalArgumentException
- if the given condition is
not associated with this lockNullPointerException
- if the condition is nullIgniteException
- if the lock is not initialized or already removedboolean isFailoverSafe()
true
if this lock is safe to use after node failure.
If not, IgniteInterruptedException is thrown on every other node after node failure.true
if this reentrant lock has failoverSafe set trueIgniteException
- if the lock is not initialized or already removedboolean isFair()
true
if this lock is fair. Fairness flag can only be set on lock creation.true
if this reentrant lock has fairness flag set true.IgniteException
- if the lock is not initialized or already removedboolean isBroken() throws IgniteException
IgniteException
- if the lock is not initialized or already removedboolean removed()
true
if reentrant lock was removed from cache, false
in other case.void close()
close
in interface AutoCloseable
close
in interface Closeable
IgniteException
- If operation failed.
Follow @ApacheIgnite
Ignite Database and Caching Platform : ver. 2.16.0 Release Date : December 15 2023