public class NonReentrantLock extends Object implements Lock
Like the name says, this lock is non-reentrant and will deadlock if re-entrance is accidentally attempted.
Since it is not reentrant (i.e. no bookkeeping that allows it to track re-entrance), this lock does not have an exclusive owner thread. This enables patterns of use where it is locked by one thread but then unlocked by another, which facilitates interesting locking protocols that support thread-skips when asynchronous programming styles are used.
Like normal locks, awaiting or signaling a condition requires that the lock be locked. But since the lock has no concept of an "owner", it cannot be verified that it is the awaiting or signaling thread that has locked it.
Awaiting a condition will unlock the lock before parking the awaiting thread. If that unlock
operation fails, an IllegalMonitorStateException is thrown. Signaling a condition just
does a best effort check that the lock is locked. If the lock is not locked at the onset of the
operation, an IllegalMonitorStateException is thrown. But if the lock is concurrently
unlocked by another thread, awaiting threads are still signaled.
| Constructor and Description |
|---|
NonReentrantLock()
Constructs a new, unfair lock.
|
NonReentrantLock(boolean fair)
Constructs a new lock.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
isFair()
Returns true if this is a fair lock.
|
void |
lock() |
void |
lockInterruptibly() |
Condition |
newCondition() |
boolean |
tryLock() |
boolean |
tryLock(long time,
TimeUnit unit) |
void |
unlock() |
public NonReentrantLock()
public NonReentrantLock(boolean fair)
fair - if true, the new lock is a fair lock; otherwise, it will be unfairpublic boolean isFair()
public void lockInterruptibly()
throws InterruptedException
lockInterruptibly in interface LockInterruptedExceptionpublic boolean tryLock(long time,
TimeUnit unit)
throws InterruptedException
tryLock in interface LockInterruptedExceptionpublic Condition newCondition()
newCondition in interface Lock