public interface InterProcessLock extends Lock
ChronicleHash
data: segments, entries.
This lock is not reentrant, but kind of "saturating": multiple lock()
calls have
the same effect as a single call. Likewise unlock()
-- multiple unlocks, or "unlocking"
when the lock isn't actually held, has no negative effects.
Once a lock object obtained, it shouldn't be stored in a field and accessed from multiple
threads, instead of that, lock objects should be obtained in each thread separately, using
the same call chain. This is because since the lock is inter-process, it anyway keeps the
synchronization state in shared memory, but restricting on-heap "view" of shared lock to a single
thread is beneficial form performance point-of-view, e. g. fields of the on-heap
InterProcessLock
object shouldn't be volatile
.
Locks is inter-process, hence it cannot afford to wait for acquisition infinitely, because
it would be too dead-lock prone. lock()
throws RuntimeException
after some
finite time.
InterProcessReadWriteUpdateLock
Modifier and Type | Method and Description |
---|---|
boolean |
isHeldByCurrentThread()
Checks if this lock is held by current thread.
|
void |
lock()
Acquires the lock, if this lock (or stronger-level lock, in the context of
InterProcessReadWriteUpdateLock ) is already held by current thread, this call returns
immediately. |
default Condition |
newCondition()
Conditions are not supported by inter-process locks, always throws
UnsupportedOperationException . |
boolean |
tryLock()
Acquires the lock only if it is free at the time of invocation, also if the lock is already
held by the current thread, this call immediately returns
true . |
void |
unlock()
Releases the lock, if the lock is not held by the current thread, returns immediately.
|
lockInterruptibly, tryLock
boolean isHeldByCurrentThread()
void lock()
InterProcessReadWriteUpdateLock
) is already held by current thread, this call returns
immediately.
If the lock is not available then the current thread enter a busy loop. After some
specified threshold, the thread might be disabled for thread scheduling purposes
and lies dormant until the lock has been acquired. After another specified threshold
RuntimeException
is thrown.
lock
in interface Lock
IllegalInterProcessLockStateException
- if this method call observes illegal lock
state, or some lock limitations reached (e. g. maximum read lock holders)RuntimeException
- if fails to acquire a lock for some finite timeboolean tryLock()
true
.
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
.
Example usage:
try (ExternalMapQueryContext<K, V, ?> q = map.queryContext(key)) {
if (q.updateLock().tryLock()) {
// highly-probable branch
if (q.entry() != null) {
// upgrade to write lock
q.writeLock().lock();
q.remove(q.entry());
} else {
// ...
}
} else {
// if failed to acquire the update lock without waiting, go acquire the write lock
// right away, because probability that we will need to upgrade to write lock anyway
// is high.
q.writeLock().lock();
if (q.entry() != null) {
q.remove(q.entry());
} else {
// ...
}
}
}
tryLock
in interface Lock
true
if the lock was acquired and false
otherwiseIllegalInterProcessLockStateException
- if this method call observes illegal lock
state, or some lock limitations reached (e. g. maximum read lock holders)void unlock()
unlock
in interface Lock
IllegalInterProcessLockStateException
- if this method call observes illegal lock
state@NotNull default Condition newCondition()
UnsupportedOperationException
.newCondition
in interface Lock
UnsupportedOperationException
- alwaysCopyright © 2015. All rights reserved.