Class TLcdLockUtil

java.lang.Object
com.luciad.util.concurrent.TLcdLockUtil

public class TLcdLockUtil extends Object
This class helps to acquire read and write locks associated to given objects (particularly domain models), making use of the 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.

  • 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 is true for Java 1.6 and higher, and false for Java 1.5.
      Parameters:
      aFairPolicy - true if a fair ordering policy should be used, false otherwise
      See Also:
    • readLock

      public static TLcdLockUtil.Lock readLock(Object o)
      Acquires a read lock associated to the given object. This lock must be released by 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()).

      Parameters:
      o - the given object to which the lock will be associated. Cannot be null.
      Returns:
      A Closeable that unlocks the object in its close method.
    • readUnlock

      public static void readUnlock(Object o)
      Releases a read lock associated to the given object.

      If the given object itself implements ReadWriteLock, the unlocking will be delegated to the object (by calling o.readLock().unlock()).

      Parameters:
      o - the given object to which the lock will be associated. Cannot be null.
    • writeLock

      public static TLcdLockUtil.Lock writeLock(Object o)
      Acquires a write lock associated to the given object. This lock must be released by 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()).

      Parameters:
      o - the given object to which the lock will be associated. Cannot be null.
      Returns:
      A Closeable that unlocks the object in its close method.
    • writeUnlock

      public static void writeUnlock(Object o)
      Releases a write lock associated to the given object.

      If the given object itself implements ReadWriteLock, the unlocking will be delegated to the object (by calling o.writeLock().unlock()).

      Parameters:
      o - the given object to which the lock will be associated. Cannot be null.