Package com.unboundid.util
Class CloseableLock
java.lang.Object
com.unboundid.util.CloseableLock
This class provides an implementation of a reentrant lock that can be used
with the Java try-with-resources facility. It does not implement the
java.util.concurrent.locks.Lock interface in order to ensure that it
can only be used through lock-with-resources mechanism, but it uses a
java.util.concurrent.locks.ReentrantLock behind the scenes to provide
its functionality.
Example
The following example demonstrates how to use this lock using the Java try-with-resources facility:
// Wait for up to 5 seconds to acquire the lock.
try (CloseableLock.Lock lock =
closeableLock.tryLock(5L, TimeUnit.SECONDS))
{
// NOTE: If you don't reference the lock object inside the try block, the
// compiler will issue a warning.
lock.avoidCompilerWarning();
// Do something while the lock is held. The lock will automatically be
// released once code execution leaves this block.
}
catch (final InterruptedException e)
{
// The thread was interrupted before the lock could be acquired.
}
catch (final TimeoutException)
{
// The lock could not be acquired within the specified 5-second timeout.
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classThis class provides aCloseableimplementation that may be used to unlock aCloseableLockvia Java's try-with-resources facility. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of this lock with a non-fair ordering policy.CloseableLock(boolean fair) Creates a new instance of this lock with the specified ordering policy. -
Method Summary
Modifier and TypeMethodDescriptionintRetrieves the number of holds that the current thread has on the lock.intRetrieves an estimate of the number of threads currently waiting to acquire this lock.booleanhasQueuedThread(Thread thread) Indicates whether the specified thread is currently waiting to acquire this lock, orfalseif not.booleanIndicates whether any threads are currently waiting to acquire this lock.booleanisFair()Indicates whether this lock uses fair ordering.booleanIndicates whether this lock is currently held by the current thread.booleanisLocked()Indicates whether this lock is currently held by any thread.lock()Acquires this lock, blocking until the lock is available.Acquires this lock, blocking until the lock is available.toString()Retrieves a string representation of this lock.Tries to acquire the lock, waiting up to the specified length of time for it to become available.
-
Constructor Details
-
CloseableLock
public CloseableLock()Creates a new instance of this lock with a non-fair ordering policy. -
CloseableLock
Creates a new instance of this lock with the specified ordering policy.- Parameters:
fair- Indicates whether the lock should use fair ordering. Iftrue, then if multiple threads are waiting on the lock, then the one that has been waiting the longest is the one that will get it. Iffalse, then no guarantee will be made about the order. Fair ordering can incur a performance penalty.
-
-
Method Details
-
lock
Acquires this lock, blocking until the lock is available.- Returns:
- The
CloseableLock.Lockinstance that may be used to perform the unlock via the try-with-resources facility.
-
lockInterruptibly
Acquires this lock, blocking until the lock is available.- Returns:
- The
CloseableLock.Lockinstance that may be used to perform the unlock via the try-with-resources facility. - Throws:
InterruptedException- If the thread is interrupted while waiting to acquire the lock.
-
tryLock
@NotNull public CloseableLock.Lock tryLock(long waitTime, @NotNull TimeUnit timeUnit) throws InterruptedException, TimeoutException Tries to acquire the lock, waiting up to the specified length of time for it to become available.- Parameters:
waitTime- The maximum length of time to wait for the lock. It must be greater than zero.timeUnit- The time unit that should be used when evaluating thewaitTimevalue.- Returns:
- The
CloseableLock.Lockinstance that may be used to perform the unlock via the try-with-resources facility. - Throws:
InterruptedException- If the thread is interrupted while waiting to acquire the lock.TimeoutException- If the lock could not be acquired within the specified length of time.
-
isFair
Indicates whether this lock uses fair ordering.- Returns:
trueif this lock uses fair ordering, orfalseif not.
-
isLocked
Indicates whether this lock is currently held by any thread.- Returns:
trueif this lock is currently held by any thread, orfalseif not.
-
isHeldByCurrentThread
Indicates whether this lock is currently held by the current thread.- Returns:
trueif this lock is currently held by the current thread, orfalseif not.
-
getHoldCount
Retrieves the number of holds that the current thread has on the lock.- Returns:
- The number of holds that the current thread has on the lock.
-
hasQueuedThreads
Indicates whether any threads are currently waiting to acquire this lock.- Returns:
trueif any threads are currently waiting to acquire this lock, orfalseif not.
-
hasQueuedThread
Indicates whether the specified thread is currently waiting to acquire this lock, orfalseif not.- Parameters:
thread- The thread for which to make the determination. It must not benull.- Returns:
trueif the specified thread is currently waiting to acquire this lock, orfalseif not.
-
getQueueLength
Retrieves an estimate of the number of threads currently waiting to acquire this lock.- Returns:
- An estimate of the number of threads currently waiting to acquire this lock.
-
toString
Retrieves a string representation of this lock.
-