Package de.gsi.dataset.locks
Class DefaultDataSetLock<D extends DataSet>
- java.lang.Object
-
- de.gsi.dataset.locks.DefaultDataSetLock<D>
-
- 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 awriteLock()and subsequentwriteUnLock()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 stateHowever, 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 processingResult 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 Summary
Constructors Constructor Description DefaultDataSetLock(D dataSet)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description DdownGradeWriteLock()Deprecated.do not use (yet)longgetLastReadStamp()booleangetLastStoredAutoNotificationState()longgetLastWriteStamp()java.util.concurrent.locks.StampedLockgetLockObject()intgetReaderCount()intgetWriterCount()DreadLock()reentrant read-lockDreadLockGuard(java.lang.Runnable reading)<R> RreadLockGuard(java.util.function.Supplier<R> reading)DreadLockGuardOptimistic(java.lang.Runnable reading)<R> RreadLockGuardOptimistic(java.util.function.Supplier<R> reading)DreadUnLock()protected booleanthreadsAreUnequal(java.lang.Thread thread1, java.lang.Thread thread2)DwriteLock()DwriteLockGuard(java.lang.Runnable writing)<R> RwriteLockGuard(java.util.function.Supplier<R> writing)DwriteUnLock()
-
-
-
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:DataSetLockreentrant read-lock- Specified by:
readLockin interfaceDataSetLock<D extends DataSet>- Returns:
- supporting DataSet (fluent design)
-
readLockGuard
public D readLockGuard(java.lang.Runnable reading)
- Specified by:
readLockGuardin interfaceDataSetLock<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:
readLockGuardin interfaceDataSetLock<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:
readLockGuardOptimisticin interfaceDataSetLock<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:
readLockGuardOptimisticin interfaceDataSetLock<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:
readUnLockin interfaceDataSetLock<D extends DataSet>- Returns:
- supporting DataSet (fluent design)
-
writeLock
public D writeLock()
- Specified by:
writeLockin interfaceDataSetLock<D extends DataSet>- Returns:
- supporting DataSet (fluent design)
-
writeLockGuard
public D writeLockGuard(java.lang.Runnable writing)
- Specified by:
writeLockGuardin interfaceDataSetLock<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:
writeLockGuardin interfaceDataSetLock<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)
-
writeUnLock
public D writeUnLock()
- Specified by:
writeUnLockin interfaceDataSetLock<D extends DataSet>- Returns:
- supporting DataSet (fluent design)
-
threadsAreUnequal
protected boolean threadsAreUnequal(java.lang.Thread thread1, java.lang.Thread thread2)
-
-