Home

ReentrantLock

ReentrantLock is an explicit mutual exclusion lock in Java's java.util.concurrent.locks package. It implements the Lock interface and offers features beyond the built-in synchronized keyword, including reentrancy, fairness control, and associated Condition objects for complex wait/notify patterns. A ReentrantLock can be held by at most one thread at a time, and it exposes methods for locking, unlocking, and querying the lock state.

The lock is reentrant: if a thread currently holds the lock, it may acquire it again without

Fairness can be configured at construction time. ReentrantLock(boolean fair) creates either a fair lock, which favors

Core usage patterns include lock(), which acquires the lock, and unlock(), which releases it. Unlock must be

Condition objects created via newCondition() are associated with the lock and enable advanced wait/notify semantics through

In practice, ReentrantLock offers flexible locking and often better scalability than intrinsic synchronization, while requiring disciplined

blocking.
The
lock
keeps
a
hold
count
per
owner
thread,
and
the
thread
must
release
the
lock
the
same
number
of
times
it
acquired
it.
the
longest-waiting
thread,
or
a
non-fair
lock
with
potentially
higher
throughput.
called
by
the
owning
thread;
unlocking
by
a
non-owner
throws
IllegalMonitorStateException.
The
class
also
provides
tryLock()
for
a
non-blocking
attempt,
and
tryLock(long
timeout,
TimeUnit
unit)
to
wait
up
to
a
specified
time.
lockInterruptibly()
allows
a
waiting
thread
to
respond
to
interrupts.
await(),
signal(),
and
signalAll().
use
of
finally
blocks
to
release
locks
and
proper
handling
of
interruptions
and
exceptions.
It
also
provides
memory
visibility
guarantees:
actions
performed
under
the
lock
happen-before
actions
after
a
successful
lock
by
another
thread.