Package com.unboundid.util
Class CloseableLock
- java.lang.Object
-
- com.unboundid.util.CloseableLock
-
@Mutable @ThreadSafety(level=COMPLETELY_THREADSAFE) public final class CloseableLock extends java.lang.Object
This class provides an implementation of a reentrant lock that can be used with the Java try-with-resources facility. It does not implement thejava.util.concurrent.locks.Lockinterface in order to ensure that it can only be used through lock-with-resources mechanism, but it uses ajava.util.concurrent.locks.ReentrantLockbehind 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 Classes Modifier and Type Class Description classCloseableLock.LockThis class provides aCloseableimplementation that may be used to unlock aCloseableLockvia Java's try-with-resources facility.
-
Constructor Summary
Constructors Constructor Description CloseableLock()Creates 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetHoldCount()Retrieves the number of holds that the current thread has on the lock.intgetQueueLength()Retrieves an estimate of the number of threads currently waiting to acquire this lock.booleanhasQueuedThread(java.lang.Thread thread)Indicates whether the specified thread is currently waiting to acquire this lock, orfalseif not.booleanhasQueuedThreads()Indicates whether any threads are currently waiting to acquire this lock.booleanisFair()Indicates whether this lock uses fair ordering.booleanisHeldByCurrentThread()Indicates whether this lock is currently held by the current thread.booleanisLocked()Indicates whether this lock is currently held by any thread.CloseableLock.Locklock()Acquires this lock, blocking until the lock is available.CloseableLock.LocklockInterruptibly()Acquires this lock, blocking until the lock is available.java.lang.StringtoString()Retrieves a string representation of this lock.CloseableLock.LocktryLock(long waitTime, java.util.concurrent.TimeUnit timeUnit)Tries to acquire the lock, waiting up to the specified length of time for it to become available.
-
-
-
Constructor Detail
-
CloseableLock
public CloseableLock()
Creates a new instance of this lock with a non-fair ordering policy.
-
CloseableLock
public CloseableLock(boolean fair)
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 Detail
-
lock
@NotNull public CloseableLock.Lock 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
@NotNull public CloseableLock.Lock lockInterruptibly() throws java.lang.InterruptedException
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:
java.lang.InterruptedException- If the thread is interrupted while waiting to acquire the lock.
-
tryLock
@NotNull public CloseableLock.Lock tryLock(long waitTime, @NotNull java.util.concurrent.TimeUnit timeUnit) throws java.lang.InterruptedException, java.util.concurrent.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:
java.lang.InterruptedException- If the thread is interrupted while waiting to acquire the lock.java.util.concurrent.TimeoutException- If the lock could not be acquired within the specified length of time.
-
isFair
public boolean isFair()
Indicates whether this lock uses fair ordering.- Returns:
trueif this lock uses fair ordering, orfalseif not.
-
isLocked
public boolean isLocked()
Indicates whether this lock is currently held by any thread.- Returns:
trueif this lock is currently held by any thread, orfalseif not.
-
isHeldByCurrentThread
public boolean 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
public int 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
public boolean 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
public boolean hasQueuedThread(@NotNull java.lang.Thread thread)
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
public int 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.
-
-