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.
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.
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 Summary
ConstructorsConstructorDescriptionConstructs a CloseableReentrantLock with the default fairness policy of false.CloseableReentrantLock(boolean fair) Constructs a CloseableReentrantLock with the given fairness policy. -
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(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
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.
-