public class NonReentrantLock extends Object implements Lock, Serializable
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.
Constructor and Description |
---|
NonReentrantLock() |
Modifier and Type | Method and Description |
---|---|
protected Thread |
getOwner()
Returns the thread that currently owns this lock, or
null if not owned. |
protected Collection<Thread> |
getQueuedThreads()
Returns a collection containing threads that may be waiting to acquire this lock.
|
int |
getQueueLength()
Returns an estimate of the number of threads waiting to acquire this lock.
|
protected Collection<Thread> |
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 |
hasQueuedThreads()
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 |
isHeldByCurrentThread()
Queries if this lock is held by the current thread.
|
boolean |
isLocked()
Queries if this lock is held by any thread.
|
void |
lock() |
void |
lockInterruptibly() |
Condition |
newCondition() |
String |
toString()
Returns a string identifying this lock, as well as its lock state.
|
boolean |
tryLock() |
boolean |
tryLock(long time,
TimeUnit unit) |
void |
unlock() |
public void lockInterruptibly() throws InterruptedException
lockInterruptibly
in interface Lock
InterruptedException
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException
tryLock
in interface Lock
InterruptedException
public Condition newCondition()
newCondition
in interface Lock
public boolean isHeldByCurrentThread()
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(); } } } }
true
if current thread holds this lock and false
otherwisepublic boolean isLocked()
true
if any thread holds this lock and false
otherwiseprotected Thread getOwner()
null
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 momentarily null
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.null
if not ownedpublic final boolean hasQueuedThreads()
true
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.true
if there may be other threads waiting to acquire the lockpublic final boolean hasQueuedThread(Thread thread)
true
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.thread
- the threadtrue
if the given thread is queued waiting for this lockNullPointerException
- if the thread is nullpublic final int getQueueLength()
protected Collection<Thread> getQueuedThreads()
public boolean hasWaiters(Condition condition)
true
return does not
guarantee that a future signal
will awaken any threads. This method is designed
primarily for use in monitoring of the system state.condition
- the conditiontrue
if there are any waiting threadsIllegalMonitorStateException
- if this lock is not heldIllegalArgumentException
- if the given condition is not associated with this lockNullPointerException
- if the condition is nullpublic int getWaitQueueLength(Condition condition)
condition
- the conditionIllegalMonitorStateException
- if this lock is not heldIllegalArgumentException
- if the given condition is not associated with this lockNullPointerException
- if the condition is nullprotected Collection<Thread> getWaitingThreads(Condition condition)
condition
- the conditionIllegalMonitorStateException
- if this lock is not heldIllegalArgumentException
- if the given condition is not associated with this lockNullPointerException
- if the condition is null