ReentrantLock
ReentrantLock is an explicit lock in java.util.concurrent. The same thread may acquire it more than once. Unlike synchronized, you can try to lock, lock with a timeout, lock interruptibly, and create multiple condition queues.
Detailed Explanation
synchronized is enough for a private critical section. It cannot try once and move on, and it cannot wait on two different conditions without notifyAll tricks. ReentrantLock is a lock you call: lock.lock() ... lock.unlock() in a finally. Forgetting finally is a leaked lock and a deadlock for everyone else.
Reentrancy matches synchronized: a thread that holds the lock can acquire it again. The hold count decrements on each unlock. Fair mode (new ReentrantLock(true)) grants the lock in queue order and can reduce starvation at the cost of throughput. The default is unfair and usually faster.
tryLock() and tryLock(time, unit) let you back off. lockInterruptibly() throws if the thread is interrupted while waiting, which synchronized cannot do (it only interrupts wait/sleep, not monitor entry). newCondition() returns a Condition with await/signal/signalAll. One lock can have notEmpty and notFull conditions, so you wake the right waiters.
You reach for ReentrantLock when you need those extras, or when the lock must be a field you pass around. You keep synchronized when the block is small and you want less ceremony. You keep concurrent collections when they already serialize the update.
The cost of a forgotten unlock is a system that freezes later, far from the bug. The cost of locking in a different order than another site is deadlock, the same as synchronized. The cost of a fair lock on a hot path is measurable. Do not use ReentrantLock to look more concurrent; use it when you need the API.
isHeldByCurrentThread and getHoldCount help assertions. Do not use them for control flow in production logic.
Code Examples
import java.util.concurrent.locks.ReentrantLock;
public class Box {
private final ReentrantLock lock = new ReentrantLock();
private int n;
public void inc() {
lock.lock();
try {
n++;
} finally {
lock.unlock(); // always
}
}
public int get() {
lock.lock();
try {
return n;
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
Box b = new Box();
b.inc();
System.out.println(b.get()); // prints: 1
}
}import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class Try {
public static void main(String[] args) throws InterruptedException {
ReentrantLock lock = new ReentrantLock();
if (lock.tryLock(10, TimeUnit.MILLISECONDS)) {
try {
System.out.println("got"); // prints: got
} finally {
lock.unlock();
}
}
}
}Common Gotchas
Unlock must sit in `finally`
an exception otherwise leaves the lock held forever.
Unlock from the wrong thread throws `IllegalMonitorStateException`
only the owner unlocks.
Fair locks are slower
turn fairness on only when you have a starvation problem.
Two locks still deadlock if acquired in opposite orders
Two locks still deadlock if acquired in opposite orders