Package host.anzo.commons.concurrent
Class CloseableReentrantLock
java.lang.Object
java.util.concurrent.locks.ReentrantLock
host.anzo.commons.concurrent.CloseableReentrantLock
- All Implemented Interfaces:
Serializable,AutoCloseable,Lock
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.
This lock is fair, meaning that threads will acquire the lock in the order they requested it.
- Since:
- 20.11.2016
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new CloseableReentrantLock with fairness set to true. -
Method Summary
Methods inherited from class java.util.concurrent.locks.ReentrantLock
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, lock, lockInterruptibly, newCondition, toString, tryLock, tryLock, unlock
-
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
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:
closein interfaceAutoCloseable- Throws:
IllegalMonitorStateException- if the current thread does not hold the lock.
-