Class CloseableReentrantLock

java.lang.Object
java.util.concurrent.locks.ReentrantLock
host.anzo.commons.concurrent.CloseableReentrantLock
All Implemented Interfaces:
Serializable, AutoCloseable, Lock

public class CloseableReentrantLock extends ReentrantLock implements AutoCloseable
A CloseableReentrantLock is a specialized implementation of a ReentrantLock that implements the AutoCloseable interface. This allows the lock to be used in a try-with-resources statement, ensuring that the lock is properly released when it is no longer needed.

The default fairness policy of the lock is false, meaning that threads will acquire the lock in the order of their arrival.

If the fairness policy is set to true, threads will acquire the lock in the order they requested it.
Since:
20.11.2016
See Also:
  • Constructor Details

    • CloseableReentrantLock

      public CloseableReentrantLock(boolean fair)
      Constructs a CloseableReentrantLock with the given fairness policy.

      If the fairness policy is set to true, threads will acquire the lock in the order they requested it.
      Parameters:
      fair - the fairness policy of the lock
    • CloseableReentrantLock

      public CloseableReentrantLock()
      Constructs a CloseableReentrantLock with the default fairness policy of false.

      This means that threads will acquire the lock in the order of their arrival.

      Note that setting the fairness policy to true may have a significant impact on performance, as it requires more atomic operations to ensure fairness.
  • Method Details

    • open

      public CloseableReentrantLock open()
      Acquires the lock, blocking until it is available.
      Returns:
      this instance of CloseableReentrantLock, allowing for method chaining.
    • close

      public void close()
      Releases the lock. This method is called automatically when the try-with-resources statement is exited.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IllegalMonitorStateException - if the current thread does not hold the lock.