public class TLcdLockUtil extends Object
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.
Modifier and Type | Class and Description |
---|---|
static interface |
TLcdLockUtil.Lock
A
Closeable that unlocks an object in its TLcdLockUtil.Lock.close() method. |
Constructor and Description |
---|
TLcdLockUtil() |
Modifier and Type | Method and Description |
---|---|
static boolean |
isFairPolicy()
Returns if the created locks use a fair ordering policy.
|
static TLcdLockUtil.Lock |
readLock(Object o)
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 |
writeLock(Object o)
Acquires a write lock associated to the given object.
|
static void |
writeUnlock(Object o)
Releases a write lock associated to the given object.
|
public static boolean isFairPolicy()
ReentrantReadWriteLock
,
setFairPolicy(boolean)
public static void setFairPolicy(boolean aFairPolicy)
true
for Java 1.6 and higher,
and false
for Java 1.5.aFairPolicy
- true if a fair ordering policy should be used, false otherwiseReentrantReadWriteLock
,
isFairPolicy()
public static TLcdLockUtil.Lock readLock(Object o)
readUnlock(Object)
, or using the Java 1.7 try-with-resources idiom (see example
).
If the given object itself implements ReadWriteLock
, the
locking will be delegated to the object (by calling o.readLock().lock()
).public static void readUnlock(Object o)
ReadWriteLock
, the
unlocking will be delegated to the object (by calling o.readLock().unlock()
).o
- the given object to which the lock will be associated. Cannot be null.public static TLcdLockUtil.Lock writeLock(Object o)
writeUnlock(Object)
, or using the Java 1.7 try-with-resources idiom (see example
).
If the given object itself implements ReadWriteLock
, the
locking will be delegated to the object (by calling o.writeLock().lock()
).public static void writeUnlock(Object o)
ReadWriteLock
, the
unlocking will be delegated to the object (by calling o.writeLock().unlock()
).o
- the given object to which the lock will be associated. Cannot be null.