Class DefaultDataSetLock<D extends DataSet>

  • Type Parameters:
    D - generics reference, usually to <? extends DataSet>
    All Implemented Interfaces:
    DataSetLock<D>, java.io.Serializable

    public class DefaultDataSetLock<D extends DataSet>
    extends java.lang.Object
    implements DataSetLock<D>
    A Simple ReadWriteLock for the DataSet interface and its fluent-design approach Some implementation recommendation: write lock guards behave the same as ReentrantLock with the additional functionality, that a writeLock() and subsequent writeUnLock() mute and, respectively, un-mute the given DataSet's auto-notification states, e.g. example:
      lock.writeLock(); // stores isAutoNotification state
         [..] some other code [..]
      lock.writeUnLock(); // restores isAutoNotification state
     
    However, the recommended usage is using the lock guard primitives, e.g.
     lock.readLockGuard(() -> {
        [..] some read-lock protected code [..]
        return retVal; // N.B. optional return - here: assumes Objects or boxed primitives
     });
     
    Alternatively the best performing option for frequent simple reads without major data processing
     Result ret = lock.readLockGuardOptimistic(() -> {
        [..] some read-lock protected code [..]
              return retVal; // N.B. optional return - here: assumes Objects or boxed primitives
     });
     
    The latter assumes infrequent writes (e.g. a single writer thread) and frequent unobstructed reads (ie. many reader threads). The lock internally acquires the data w/o explicitly locking, checks afterwards if the data has potentially changed a write-lock acquiring thread, and as a automatic fall-back uses the guaranteed (but more expensive) read lock to assure that the read data structure is consistent.
    Author:
    rstein
    See Also:
    Serialized Form
    • Constructor Detail

      • DefaultDataSetLock

        public DefaultDataSetLock​(D dataSet)
        Parameters:
        dataSet - dataSet this set is associate with
    • Method Detail

      • downGradeWriteLock

        @Deprecated
        public D downGradeWriteLock()
        Deprecated.
        do not use (yet)
        experimental down-grading of the writer lock
        Returns:
        corresponding data set
      • getLastReadStamp

        public long getLastReadStamp()
        Returns:
        last reader stamp
        See Also:
        StampedLock
      • getLastStoredAutoNotificationState

        public boolean getLastStoredAutoNotificationState()
        Returns:
        the last stored auto-notification state
      • getLastWriteStamp

        public long getLastWriteStamp()
        Returns:
        last writer stamp
        See Also:
        StampedLock
      • getLockObject

        public java.util.concurrent.locks.StampedLock getLockObject()
        Returns:
        the internal StampedLock object
      • getReaderCount

        public int getReaderCount()
        Returns:
        number of readers presently locked on this data set
      • getWriterCount

        public int getWriterCount()
        Returns:
        number of writers presently locked on this data set (N.B. all from the same thread)
      • readLock

        public D readLock()
        Description copied from interface: DataSetLock
        reentrant read-lock
        Specified by:
        readLock in interface DataSetLock<D extends DataSet>
        Returns:
        supporting DataSet (fluent design)
      • readLockGuard

        public D readLockGuard​(java.lang.Runnable reading)
        Specified by:
        readLockGuard in interface DataSetLock<D extends DataSet>
        Parameters:
        reading - typ. lambda expression that is executed with read lock
        Returns:
        supporting DataSet (fluent design)
      • readLockGuard

        public <R> R readLockGuard​(java.util.function.Supplier<R> reading)
        Specified by:
        readLockGuard in interface DataSetLock<D extends DataSet>
        Type Parameters:
        R - generic return type
        Parameters:
        reading - typ. lambda expression that is executed with read lock
        Returns:
        supporting DataSet (fluent design)
      • readLockGuardOptimistic

        public D readLockGuardOptimistic​(java.lang.Runnable reading)
        Specified by:
        readLockGuardOptimistic in interface DataSetLock<D extends DataSet>
        Parameters:
        reading - typ. lambda expression that is executed with read lock
        Returns:
        supporting DataSet (fluent design)
      • readLockGuardOptimistic

        public <R> R readLockGuardOptimistic​(java.util.function.Supplier<R> reading)
        Specified by:
        readLockGuardOptimistic in interface DataSetLock<D extends DataSet>
        Type Parameters:
        R - generic return type
        Parameters:
        reading - typ. lambda expression that is executed with read lock
        Returns:
        supporting DataSet (fluent design)
      • readUnLock

        public D readUnLock()
        Specified by:
        readUnLock in interface DataSetLock<D extends DataSet>
        Returns:
        supporting DataSet (fluent design)
      • writeLock

        public D writeLock()
        Specified by:
        writeLock in interface DataSetLock<D extends DataSet>
        Returns:
        supporting DataSet (fluent design)
      • writeLockGuard

        public D writeLockGuard​(java.lang.Runnable writing)
        Specified by:
        writeLockGuard in interface DataSetLock<D extends DataSet>
        Parameters:
        writing - typ. lambda expression that is executed with write lock
        Returns:
        supporting DataSet (fluent design)
      • writeLockGuard

        public <R> R writeLockGuard​(java.util.function.Supplier<R> writing)
        Specified by:
        writeLockGuard in interface DataSetLock<D extends DataSet>
        Type Parameters:
        R - generic return type
        Parameters:
        writing - typ. lambda expression that is executed with write lock
        Returns:
        supporting DataSet (fluent design)
      • threadsAreUnequal

        protected boolean threadsAreUnequal​(java.lang.Thread thread1,
                                            java.lang.Thread thread2)