Class CloseableLock

java.lang.Object
com.unboundid.util.CloseableLock

This class provides an implementation of a reentrant lock that can be used with the Java try-with-resources facility. It does not implement the java.util.concurrent.locks.Lock interface in order to ensure that it can only be used through lock-with-resources mechanism, but it uses a java.util.concurrent.locks.ReentrantLock behind the scenes to provide its functionality.

Example

The following example demonstrates how to use this lock using the Java try-with-resources facility:
 // Wait for up to 5 seconds to acquire the lock.
 try (CloseableLock.Lock lock =
           closeableLock.tryLock(5L, TimeUnit.SECONDS))
 {
   // NOTE:  If you don't reference the lock object inside the try block, the
   // compiler will issue a warning.
   lock.avoidCompilerWarning();

   // Do something while the lock is held.  The lock will automatically be
   // released once code execution leaves this block.
 }
 catch (final InterruptedException e)
 {
   // The thread was interrupted before the lock could be acquired.
 }
 catch (final TimeoutException)
 {
   // The lock could not be acquired within the specified 5-second timeout.
 }
 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    final class 
    This class provides a Closeable implementation that may be used to unlock a CloseableLock via Java's try-with-resources facility.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance of this lock with a non-fair ordering policy.
    CloseableLock(boolean fair)
    Creates a new instance of this lock with the specified ordering policy.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Retrieves the number of holds that the current thread has on the lock.
    int
    Retrieves an estimate of the number of threads currently waiting to acquire this lock.
    boolean
    Indicates whether the specified thread is currently waiting to acquire this lock, or false if not.
    boolean
    Indicates whether any threads are currently waiting to acquire this lock.
    boolean
    Indicates whether this lock uses fair ordering.
    boolean
    Indicates whether this lock is currently held by the current thread.
    boolean
    Indicates whether this lock is currently held by any thread.
    Acquires this lock, blocking until the lock is available.
    Acquires this lock, blocking until the lock is available.
    Retrieves a string representation of this lock.
    tryLock(long waitTime, TimeUnit timeUnit)
    Tries to acquire the lock, waiting up to the specified length of time for it to become available.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • CloseableLock

      public CloseableLock()
      Creates a new instance of this lock with a non-fair ordering policy.
    • CloseableLock

      public CloseableLock(boolean fair)
      Creates a new instance of this lock with the specified ordering policy.
      Parameters:
      fair - Indicates whether the lock should use fair ordering. If true, then if multiple threads are waiting on the lock, then the one that has been waiting the longest is the one that will get it. If false, then no guarantee will be made about the order. Fair ordering can incur a performance penalty.
  • Method Details

    • lock

      Acquires this lock, blocking until the lock is available.
      Returns:
      The CloseableLock.Lock instance that may be used to perform the unlock via the try-with-resources facility.
    • lockInterruptibly

      Acquires this lock, blocking until the lock is available.
      Returns:
      The CloseableLock.Lock instance that may be used to perform the unlock via the try-with-resources facility.
      Throws:
      InterruptedException - If the thread is interrupted while waiting to acquire the lock.
    • tryLock

      Tries to acquire the lock, waiting up to the specified length of time for it to become available.
      Parameters:
      waitTime - The maximum length of time to wait for the lock. It must be greater than zero.
      timeUnit - The time unit that should be used when evaluating the waitTime value.
      Returns:
      The CloseableLock.Lock instance that may be used to perform the unlock via the try-with-resources facility.
      Throws:
      InterruptedException - If the thread is interrupted while waiting to acquire the lock.
      TimeoutException - If the lock could not be acquired within the specified length of time.
    • isFair

      public boolean isFair()
      Indicates whether this lock uses fair ordering.
      Returns:
      true if this lock uses fair ordering, or false if not.
    • isLocked

      public boolean isLocked()
      Indicates whether this lock is currently held by any thread.
      Returns:
      true if this lock is currently held by any thread, or false if not.
    • isHeldByCurrentThread

      public boolean isHeldByCurrentThread()
      Indicates whether this lock is currently held by the current thread.
      Returns:
      true if this lock is currently held by the current thread, or false if not.
    • getHoldCount

      public int getHoldCount()
      Retrieves the number of holds that the current thread has on the lock.
      Returns:
      The number of holds that the current thread has on the lock.
    • hasQueuedThreads

      public boolean hasQueuedThreads()
      Indicates whether any threads are currently waiting to acquire this lock.
      Returns:
      true if any threads are currently waiting to acquire this lock, or false if not.
    • hasQueuedThread

      public boolean hasQueuedThread(@NotNull Thread thread)
      Indicates whether the specified thread is currently waiting to acquire this lock, or false if not.
      Parameters:
      thread - The thread for which to make the determination. It must not be null.
      Returns:
      true if the specified thread is currently waiting to acquire this lock, or false if not.
    • getQueueLength

      public int getQueueLength()
      Retrieves an estimate of the number of threads currently waiting to acquire this lock.
      Returns:
      An estimate of the number of threads currently waiting to acquire this lock.
    • toString

      Retrieves a string representation of this lock.
      Overrides:
      toString in class Object
      Returns:
      A string representation of this lock.