Class ReadWriteLockSync

  • All Implemented Interfaces:
    Sync

    public class ReadWriteLockSync
    extends java.lang.Object
    implements Sync
    A simple ReadWriteLock synchronizer.
    Author:
    Alex Snaps
    • Constructor Detail

      • ReadWriteLockSync

        public ReadWriteLockSync()
        default constructor.
      • ReadWriteLockSync

        public ReadWriteLockSync​(java.util.concurrent.locks.ReentrantReadWriteLock lock)
        Constructor.
        Parameters:
        lock -
    • Method Detail

      • lock

        public void lock​(LockType type)
        Acquire lock of LockType.READ or WRITE
        Specified by:
        lock in interface Sync
        Parameters:
        type - the lock type to acquire
      • tryLock

        public boolean tryLock​(LockType type,
                               long msec)
                        throws java.lang.InterruptedException
        Tries to acquire a LockType.READ or WRITE for a certain period
        Specified by:
        tryLock in interface Sync
        Parameters:
        type - the lock type to acquire
        msec - timeout
        Returns:
        true if the lock got acquired, false otherwise
        Throws:
        java.lang.InterruptedException - Should the thread be interrupted
      • unlock

        public void unlock​(LockType type)
        Releases the lock held by the current Thread. In case of a LockType.WRITE, should the lock not be held by the current Thread, nothing happens
        Specified by:
        unlock in interface Sync
        Parameters:
        type - the lock type to acquire
      • getReadWriteLock

        public java.util.concurrent.locks.ReadWriteLock getReadWriteLock()
        Gets the ReadWriteLock backing this sync.
        Returns:
        the backing ReadWriteLock
      • isHeldByCurrentThread

        public boolean isHeldByCurrentThread​(LockType type)
        Returns true is this is lock is held at given level by the current thread.
        Specified by:
        isHeldByCurrentThread in interface Sync
        Parameters:
        type - the lock type to test
        Returns:
        true if the lock is held