Class KeyedLock<T>

java.lang.Object
org.elasticsearch.common.util.concurrent.KeyedLock<T>

public final class KeyedLock<T> extends Object
This class manages locks. Locks can be accessed with an identifier and are created the first time they are acquired and removed if no thread hold the lock. The latter is important to assure that the list of locks does not grow infinitely. Note: this lock is reentrant
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a non-fair lock
    KeyedLock(boolean fair)
    Creates a new lock
  • Method Summary

    Modifier and Type
    Method
    Description
    org.elasticsearch.core.Releasable
    acquire(T key)
    Acquires a lock for the given key.
    boolean
    Returns true if this lock has at least one locked key.
    boolean
    Returns true iff the caller thread holds the lock for the given key
    org.elasticsearch.core.Releasable
    Tries to acquire the lock for the given key and returns it.

    Methods inherited from class java.lang.Object

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

    • KeyedLock

      public KeyedLock(boolean fair)
      Creates a new lock
      Parameters:
      fair - Use fair locking, ie threads get the lock in the order they requested it
    • KeyedLock

      public KeyedLock()
      Creates a non-fair lock
  • Method Details

    • acquire

      public org.elasticsearch.core.Releasable acquire(T key)
      Acquires a lock for the given key. The key is compared by it's equals method not by object identity. The lock can be acquired by the same thread multiple times. The lock is released by closing the returned Releasable.
    • tryAcquire

      public org.elasticsearch.core.Releasable tryAcquire(T key)
      Tries to acquire the lock for the given key and returns it. If the lock can't be acquired null is returned.
    • isHeldByCurrentThread

      public boolean isHeldByCurrentThread(T key)
      Returns true iff the caller thread holds the lock for the given key
    • hasLockedKeys

      public boolean hasLockedKeys()
      Returns true if this lock has at least one locked key.