Class ReadWriteSynchronizer

  • All Implemented Interfaces:
    Synchronizer

    public class ReadWriteSynchronizer
    extends java.lang.Object
    implements Synchronizer

    A special implementation of Synchronizer based on the JDK's ReentrantReadWriteLock class.

    This class manages a ReadWriteLock object internally. The methods of the Synchronizer interface are delegated to this lock. So this class behaves in the same way as documented for ReentrantReadWriteLock.

    Using this Synchronizer implementation is appropriate to make configuration objects thread-safe. This means that multiple threads can read configuration data in parallel; if one thread wants to update the configuration, this happens with an exclusive lock.

    Since:
    2.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.locks.ReadWriteLock lock
      The lock object used by this Synchronizer.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReadWriteSynchronizer()
      Creates a new instance of ReadWriteSynchronizer and initializes it with a lock object of type ReentrantReadWriteLock.
      ReadWriteSynchronizer​(java.util.concurrent.locks.ReadWriteLock l)
      Creates a new instance of ReadWriteSynchronizer and initializes it with the given lock object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginRead()
      Notifies this Synchronizer that the current thread is going to start a read operation on the managed configuration.
      void beginWrite()
      Notifies this Synchronizer that the current thread is going to start a write operation on the managed configuration.
      private static java.util.concurrent.locks.ReadWriteLock createDefaultLock()
      Returns a new default lock object which is used if no lock is passed to the constructor.
      void endRead()
      Notifies this Synchronizer that the current thread has finished its read operation.
      void endWrite()
      Notifies this Synchronizer that the current thread has finished its write operation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • lock

        private final java.util.concurrent.locks.ReadWriteLock lock
        The lock object used by this Synchronizer.
    • Constructor Detail

      • ReadWriteSynchronizer

        public ReadWriteSynchronizer()
        Creates a new instance of ReadWriteSynchronizer and initializes it with a lock object of type ReentrantReadWriteLock.
      • ReadWriteSynchronizer

        public ReadWriteSynchronizer​(java.util.concurrent.locks.ReadWriteLock l)
        Creates a new instance of ReadWriteSynchronizer and initializes it with the given lock object. This constructor can be used to pass a lock object which has been configured externally. If the lock object is null, a default lock object is created.
        Parameters:
        l - the lock object to be used (can be null)
    • Method Detail

      • createDefaultLock

        private static java.util.concurrent.locks.ReadWriteLock createDefaultLock()
        Returns a new default lock object which is used if no lock is passed to the constructor.
        Returns:
        the new default lock object
      • beginRead

        public void beginRead()
        Description copied from interface: Synchronizer
        Notifies this Synchronizer that the current thread is going to start a read operation on the managed configuration. This call can block if a concrete implementation decides that the thread has to wait until a specific condition is fulfilled.
        Specified by:
        beginRead in interface Synchronizer
      • beginWrite

        public void beginWrite()
        Description copied from interface: Synchronizer
        Notifies this Synchronizer that the current thread is going to start a write operation on the managed configuration. This call may block. For instance, a concrete implementation may suspend the thread until all read operations currently active are finished,
        Specified by:
        beginWrite in interface Synchronizer
      • endRead

        public void endRead()
        Description copied from interface: Synchronizer
        Notifies this Synchronizer that the current thread has finished its read operation. This may cause other waiting threads to be granted access to the managed configuration.
        Specified by:
        endRead in interface Synchronizer
      • endWrite

        public void endWrite()
        Description copied from interface: Synchronizer
        Notifies this Synchronizer that the current thread has finished its write operation. This may cause other waiting threads to be granted access to the managed configuration.
        Specified by:
        endWrite in interface Synchronizer