Package com.unboundid.util
Class CloseableReadWriteLock
java.lang.Object
com.unboundid.util.CloseableReadWriteLock
@Mutable
@ThreadSafety(level=COMPLETELY_THREADSAFE)
public final class CloseableReadWriteLock
extends Object
This class provides an implementation of a reentrant read-write lock that can
be used with the Java try-with-resources facility. With a read-write lock,
either exactly one thread can hold the write lock while no other threads hold
read locks, or zero or more threads can hold read locks while no thread holds
the write lock. The one exception to this policy is that the thread that
holds the write lock can downgrade will be permitted to acquire a read lock
before it releases the write lock to downgrade from a write lock to a read
lock while ensuring that no other thread is permitted to acquire the write
lock while it is in the process of downgrading.
This class does not implement the
This class does not implement the
java.util.concurrent.locks.ReadWriteLock interface in order to ensure
that it can only be used through the try-with-resources mechanism, but it
uses a java.util.concurrent.locks.ReentrantReadWriteLock 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 (CloseableReadWriteLock.WriteLock writeLock =
closeableReadWriteLock.tryLock(5L, TimeUnit.SECONDS))
{
// NOTE: If you don't reference the lock object inside the try block, the
// compiler will issue a warning.
writeLock.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 aCloseableReadWriteLock's read lock via Java's try-with-resources facility.final classThis class provides aCloseableimplementation that may be used to unlock aCloseableReadWriteLock's write lock via Java's try-with-resources facility. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of this read-write lock with a non-fair ordering policy.CloseableReadWriteLock(boolean fair) Creates a new instance of this read-write lock with the specified ordering policy. -
Method Summary
Modifier and TypeMethodDescriptionintRetrieves an estimate of the number of threads currently waiting to acquire either the write or read lock.intRetrieves the number of holds that the current thread has on the read lock.intRetrieves the number of threads that currently hold the read lock.intRetrieves the number of holds that the current thread has on the write lock.booleanhasQueuedThread(Thread thread) Indicates whether the specified thread is currently waiting to acquire either the write or read lock.booleanIndicates whether any threads are currently waiting to acquire either the write or read lock.booleanisFair()Indicates whether this lock uses fair ordering.booleanIndicates whether the write lock is currently held by any thread.booleanIndicates whether the write lock is currently held by the current thread.lockRead()Acquires a read lock, blocking until the lock is available.Acquires a read lock, blocking until the lock is available.Acquires the write lock, blocking until the lock is available.Acquires the write lock, blocking until the lock is available.toString()Retrieves a string representation of this read-write lock.tryLockRead(long waitTime, TimeUnit timeUnit) Tries to acquire a read lock, waiting up to the specified length of time for it to become available.tryLockWrite(long waitTime, TimeUnit timeUnit) Tries to acquire the write lock, waiting up to the specified length of time for it to become available.
-
Constructor Details
-
CloseableReadWriteLock
public CloseableReadWriteLock()Creates a new instance of this read-write lock with a non-fair ordering policy. -
CloseableReadWriteLock
Creates a new instance of this read-write 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
-
lockWrite
Acquires the write lock, blocking until the lock is available.- Returns:
- The
CloseableReadWriteLock.WriteLockinstance that may be used to perform the unlock via the try-with-resources facility.
-
lockWriteInterruptibly
@NotNull public CloseableReadWriteLock.WriteLock lockWriteInterruptibly() throws InterruptedExceptionAcquires the write lock, blocking until the lock is available.- Returns:
- The
CloseableReadWriteLock.WriteLockinstance 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.
-
tryLockWrite
@NotNull public CloseableReadWriteLock.WriteLock tryLockWrite(long waitTime, @NotNull TimeUnit timeUnit) throws InterruptedException, TimeoutException Tries to acquire the write 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
CloseableReadWriteLock.WriteLockinstance 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.
-
lockRead
Acquires a read lock, blocking until the lock is available.- Returns:
- The
CloseableReadWriteLock.ReadLockinstance that may be used to perform the unlock via the try-with-resources facility.
-
lockReadInterruptibly
Acquires a read lock, blocking until the lock is available.- Returns:
- The
CloseableReadWriteLock.ReadLockinstance 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.
-
tryLockRead
@NotNull public CloseableReadWriteLock.ReadLock tryLockRead(long waitTime, @NotNull TimeUnit timeUnit) throws InterruptedException, TimeoutException Tries to acquire a read 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
CloseableReadWriteLock.ReadLockinstance 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.
-
isWriteLocked
Indicates whether the write lock is currently held by any thread.- Returns:
trueif the write lock is currently held by any thread, orfalseif not.
-
isWriteLockedByCurrentThread
Indicates whether the write lock is currently held by the current thread.- Returns:
trueif the write lock is currently held by the current thread, orfalseif not.
-
getWriteHoldCount
Retrieves the number of holds that the current thread has on the write lock.- Returns:
- The number of holds that the current thread has on the write lock.
-
getReadLockCount
Retrieves the number of threads that currently hold the read lock.- Returns:
- The number of threads that currently hold the read lock.
-
getReadHoldCount
Retrieves the number of holds that the current thread has on the read lock.- Returns:
- The number of holds that the current thread has on the read lock.
-
hasQueuedThreads
Indicates whether any threads are currently waiting to acquire either the write or read lock.- Returns:
trueif any threads are currently waiting to acquire either the write or read lock, orfalseif not.
-
hasQueuedThread
Indicates whether the specified thread is currently waiting to acquire either the write or read lock.- Parameters:
thread- The thread for which to make the determination. It must not benull.- Returns:
trueif the specified thread is currently waiting to acquire either the write or read lock, orfalseif not.
-
getQueueLength
Retrieves an estimate of the number of threads currently waiting to acquire either the write or read lock.- Returns:
- An estimate of the number of threads currently waiting to acquire either the write or read lock.
-
toString
Retrieves a string representation of this read-write lock.
-