Class PreciseMultiLock

  • All Implemented Interfaces:
    MultiLock

    public class PreciseMultiLock
    extends java.lang.Object
    implements MultiLock
    Implementation of MultiLock interface with precise locking which means no thread will block for an ID unless that ID is locked by another thread.
    Author:
    Evren Sirin
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.slf4j.Logger LOGGER  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long heldBy​(int lockID)
      Returns the thread ID that holds the lock for the given ID or 0 if it is not locked.
      void lock​(int lockID)
      Locks the given ID, waiting indefinitely if another thread already has a lock.
      java.lang.String toString()  
      boolean tryLock​(int lockID)
      Tries to lock the given ID if it is possible to do so immediately and return true.
      void unlock​(int lockID)
      Unlocks the given ID.
      • Methods inherited from class java.lang.Object

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

      • LOGGER

        public static final org.slf4j.Logger LOGGER
    • Constructor Detail

      • PreciseMultiLock

        public PreciseMultiLock()
    • Method Detail

      • lock

        public void lock​(int lockID)
        Description copied from interface: MultiLock
        Locks the given ID, waiting indefinitely if another thread already has a lock.
        Specified by:
        lock in interface MultiLock
      • tryLock

        public boolean tryLock​(int lockID)
        Description copied from interface: MultiLock
        Tries to lock the given ID if it is possible to do so immediately and return true. If the lock is already held by another thread, this function immediately returns false. Since this class does not provide reentarant behavior, this function will return false even if it is the same thread that holds the lock before this function call.
        Specified by:
        tryLock in interface MultiLock
      • heldBy

        public long heldBy​(int lockID)
        Returns the thread ID that holds the lock for the given ID or 0 if it is not locked. This function is for monitoring purposes only and should not be used for synchronization.
      • unlock

        public void unlock​(int lockID)
                    throws java.lang.IllegalArgumentException
        Description copied from interface: MultiLock
        Unlocks the given ID. There is no requirement that a thread that unlocks an ID must have locked that ID by calling MultiLock.lock(int).
        Specified by:
        unlock in interface MultiLock
        Throws:
        java.lang.IllegalArgumentException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object