Class TLcdLockUtil
java.util.concurrent.locks.ReentrantReadWriteLock
standard implementation.
Using a read lock on a model allows to perform a read operation on a model, allowing any other simultaneous reading operations, and queuing any writing operations. All operations performed between acquiring and releasing the lock will be guarded against any writing operations that have acquired a write lock for the same model.
Using a write lock on a model allows to queue writing operations and subsequent reading operations on the same model. All operations performed between acquiring and releasing the lock will be guarded against any writing and reading operations that have acquired a write lock resp. a read lock for the same model.
Example for reading data:
try (TLcdLockUtil.Lock lock = TLcdLockUtil.readLock(model);
Stream<Object> elements = model.query(all())) {
elements.forEach(consumer);
}
When you enable debug logging for this class,
it will log whenever a read/write lock is obtained/released. If you enable trace logging
,
the stack trace leading to the lock request will also be logged.
Important note : Concurrent reading and writing operations on a model always require a locking (or synchronization) strategy to avoid race conditions and inconsistent memory states. Please refer to the LuciadLightspeed Developer's Guide in order to get information about multithreading and locking in the LuciadLightspeed API.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
ACloseable
that unlocks an object in itsTLcdLockUtil.Lock.close()
method. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Returns if the created locks use a fair ordering policy.static TLcdLockUtil.Lock
Acquires a read lock associated to the given object.static void
readUnlock
(Object o) Releases a read lock associated to the given object.static void
setFairPolicy
(boolean aFairPolicy) Sets if the created locks should use a fair ordering policy.static TLcdLockUtil.Lock
Acquires a write lock associated to the given object.static void
Releases a write lock associated to the given object.
-
Constructor Details
-
TLcdLockUtil
public TLcdLockUtil()
-
-
Method Details
-
isFairPolicy
public static boolean isFairPolicy()Returns if the created locks use a fair ordering policy.- Returns:
- true if the created locks use a fair ordering policy, false otherwise
- See Also:
-
setFairPolicy
public static void setFairPolicy(boolean aFairPolicy) Sets if the created locks should use a fair ordering policy. The default value istrue
for Java 1.6 and higher, andfalse
for Java 1.5.- Parameters:
aFairPolicy
- true if a fair ordering policy should be used, false otherwise- See Also:
-
readLock
Acquires a read lock associated to the given object. This lock must be released byreadUnlock(Object)
, or using the Java 1.7 try-with-resources idiom (seeexample
). If the given object itself implementsReadWriteLock
, the locking will be delegated to the object (by callingo.readLock().lock()
). -
readUnlock
Releases a read lock associated to the given object. If the given object itself implementsReadWriteLock
, the unlocking will be delegated to the object (by callingo.readLock().unlock()
).- Parameters:
o
- the given object to which the lock will be associated. Cannot be null.
-
writeLock
Acquires a write lock associated to the given object. This lock must be released bywriteUnlock(Object)
, or using the Java 1.7 try-with-resources idiom (seeexample
). If the given object itself implementsReadWriteLock
, the locking will be delegated to the object (by callingo.writeLock().lock()
). -
writeUnlock
Releases a write lock associated to the given object. If the given object itself implementsReadWriteLock
, the unlocking will be delegated to the object (by callingo.writeLock().unlock()
).- Parameters:
o
- the given object to which the lock will be associated. Cannot be null.
-