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.

This lock is fair, meaning that threads will acquire the lock in the order they requested it.
Since:
20.11.2016
See Also:
  • Constructor Details

    • CloseableReentrantLock

      public CloseableReentrantLock()
      Constructs a new CloseableReentrantLock with fairness set to true. This ensures that the lock favors threads that are waiting to acquire it.
  • 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.