Class PreciseMultiLock

java.lang.Object
com.complexible.common.util.concurrent.locks.PreciseMultiLock
All Implemented Interfaces:
MultiLock

public class PreciseMultiLock extends 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 final org.slf4j.Logger
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    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.
     
    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 Details

    • LOGGER

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

    • PreciseMultiLock

      public PreciseMultiLock()
  • Method Details

    • 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 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:
      IllegalArgumentException
    • toString

      public String toString()
      Overrides:
      toString in class Object