Class NonReentrantLock
- All Implemented Interfaces:
Serializable
,Lock
Lock
. This type of lock does not allow recursive locks
held by the same thread and will deadlock if used recursively. This type of lock is useful when
reentrancy is not required and a slim lock is desired.
A NonReentrantLock
is owned by the thread last successfully locking, but not yet
unlocking it. A thread invoking lock
will return, successfully acquiring the lock, when
the lock is not owned by another thread. This can be checked using methods
isHeldByCurrentThread()
.
It is recommended practice to always immediately follow a call to lock
with a
try
block, most typically in a before/after construction such as:
class X {
private final ReentrantLock lock = new ReentrantLock();
// ...
public void m() {
lock.lock(); // block until condition holds
try {
// ... method body
} finally {
lock.unlock()
}
}
}
In addition to implementing the Lock
interface, this class defines a number of
public
and protected
methods for inspecting the state of the lock. Some of these
methods are only useful for instrumentation and monitoring.
Serialization of this class behaves in the same way as built-in locks: a deserialized lock is in the unlocked state, regardless of its state when serialized.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
A non-fair lock using AQS state to represent if the lock is held. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final long
(package private) final NonReentrantLock.Sync
Synchronizer providing all implementation mechanics -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetOwner()
Returns the thread that currently owns this lock, ornull
if not owned.Returns a collection containing threads that may be waiting to acquire this lock.int
Returns an estimate of the number of threads waiting to acquire this lock.getWaitingThreads
(Condition condition) Returns a collection containing those threads that may be waiting on the given condition associated with this lock.int
getWaitQueueLength
(Condition condition) Returns an estimate of the number of threads waiting on the given condition associated with this lock.boolean
hasQueuedThread
(Thread thread) Queries whether the given thread is waiting to acquire this lock.boolean
Queries whether any threads are waiting to acquire this lock.boolean
hasWaiters
(Condition condition) Queries whether any threads are waiting on the given condition associated with this lock.boolean
Queries if this lock is held by the current thread.boolean
isLocked()
Queries if this lock is held by any thread.void
lock()
void
toString()
Returns a string identifying this lock, as well as its lock state.boolean
tryLock()
boolean
void
unlock()
-
Field Details
-
serialVersionUID
static final long serialVersionUID- See Also:
-
sync
Synchronizer providing all implementation mechanics
-
-
Constructor Details
-
NonReentrantLock
public NonReentrantLock()
-
-
Method Details
-
lock
public void lock() -
lockInterruptibly
- Specified by:
lockInterruptibly
in interfaceLock
- Throws:
InterruptedException
-
tryLock
public boolean tryLock() -
tryLock
- Specified by:
tryLock
in interfaceLock
- Throws:
InterruptedException
-
unlock
public void unlock() -
newCondition
- Specified by:
newCondition
in interfaceLock
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()Queries if this lock is held by the current thread.Analogous to the
Thread.holdsLock(Object)
method for built-in monitor locks, this method is typically used for debugging and testing. For example, a method that should only be called while a lock is held can assert that this is the case:{ @code class X { ReentrantLock lock = new ReentrantLock(); // ... public void m() { assert lock.isHeldByCurrentThread(); // ... method body } } }
It can also be used to ensure that a reentrant lock is used in a non-reentrant manner, for example:
{ @code class X { ReentrantLock lock = new ReentrantLock(); // ... public void m() { assert !lock.isHeldByCurrentThread(); lock.lock(); try { // ... method body } finally { lock.unlock(); } } } }
- Returns:
true
if current thread holds this lock andfalse
otherwise
-
isLocked
public boolean isLocked()Queries if this lock is held by any thread. This method is designed for use in monitoring of the system state, not for synchronization control.- Returns:
true
if any thread holds this lock andfalse
otherwise
-
getOwner
Returns the thread that currently owns this lock, ornull
if not owned. When this method is called by a thread that is not the owner, the return value reflects a best-effort approximation of current lock status. For example, the owner may be momentarilynull
even if there are threads trying to acquire the lock but have not yet done so. This method is designed to facilitate construction of subclasses that provide more extensive lock monitoring facilities.- Returns:
- the owner, or
null
if not owned
-
hasQueuedThreads
public boolean hasQueuedThreads()Queries whether any threads are waiting to acquire this lock. Note that because cancellations may occur at any time, atrue
return does not guarantee that any other thread will ever acquire this lock. This method is designed primarily for use in monitoring of the system state.- Returns:
true
if there may be other threads waiting to acquire the lock
-
hasQueuedThread
Queries whether the given thread is waiting to acquire this lock. Note that because cancellations may occur at any time, atrue
return does not guarantee that this thread will ever acquire this lock. This method is designed primarily for use in monitoring of the system state.- Parameters:
thread
- the thread- Returns:
true
if the given thread is queued waiting for this lock- Throws:
NullPointerException
- if the thread is null
-
getQueueLength
public int getQueueLength()Returns an estimate of the number of threads waiting to acquire this lock. The value is only an estimate because the number of threads may change dynamically while this method traverses internal data structures. This method is designed for use in monitoring of the system state, not for synchronization control.- Returns:
- the estimated number of threads waiting for this lock
-
getQueuedThreads
Returns a collection containing threads that may be waiting to acquire this lock. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order. This method is designed to facilitate construction of subclasses that provide more extensive monitoring facilities.- Returns:
- the collection of threads
-
hasWaiters
Queries whether any threads are waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, atrue
return does not guarantee that a futuresignal
will awaken any threads. This method is designed primarily for use in monitoring of the system state.- Parameters:
condition
- the condition- Returns:
true
if there are any waiting threads- Throws:
IllegalMonitorStateException
- if this lock is not heldIllegalArgumentException
- if the given condition is not associated with this lockNullPointerException
- if the condition is null
-
getWaitQueueLength
Returns an estimate of the number of threads waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters. This method is designed for use in monitoring of the system state, not for synchronization control.- Parameters:
condition
- the condition- Returns:
- the estimated number of waiting threads
- Throws:
IllegalMonitorStateException
- if this lock is not heldIllegalArgumentException
- if the given condition is not associated with this lockNullPointerException
- if the condition is null
-
getWaitingThreads
Returns a collection containing those threads that may be waiting on the given condition associated with this lock. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order. This method is designed to facilitate construction of subclasses that provide more extensive condition monitoring facilities.- Parameters:
condition
- the condition- Returns:
- the collection of threads
- Throws:
IllegalMonitorStateException
- if this lock is not heldIllegalArgumentException
- if the given condition is not associated with this lockNullPointerException
- if the condition is null
-
toString
Returns a string identifying this lock, as well as its lock state. The state, in brackets, includes either the String"Unlocked"
or the String"Locked by"
followed by the name of the owning thread.
-