Sub topic 8
How Intrinsic Locks Work: Monitors and Reentrancy
In the last two chapters you used synchronized on methods and blocks. This chapter looks at what happens inside the lock that synchronized uses.
Every Object Has a Lock
An intrinsic lock is a lock that Java builds into every object. It is also called a monitor. You never create it yourself. Every object comes with one, whether it is a String, an ArrayList, or a plain new Object().
The synchronized keyword picks which lock to use:
| Code you write | Lock it uses |
|---|---|
| `synchronized` instance method | The object the method is called on (`this`) |
| `synchronized` static method | The class object, such as `Counter.class` |
| `synchronized (lock) { ... }` | The object named in the brackets |
Acquire and Release
A monitor follows three simple rules:
- When a thread enters a
synchronizedsection, it acquires (takes) the lock. - While one thread holds the lock, any other thread that wants it must block, which means it waits and does nothing.
- When the holder leaves the section, it releases the lock, even if it leaves because of an exception. One waiting thread can then take it.
Only one thread can hold a given monitor at a time. That is what makes the code inside safe.
Reentrancy: Taking a Lock You Already Hold
Reentrancy means a thread that already holds a lock can acquire it again without blocking. Java's intrinsic locks are reentrant.
Why does this matter? Look at a synchronized method that calls another synchronized method on the same object:
public class Account {
private int balance = 0;
public synchronized void deposit(int amount) {
balance += amount;
log();
}
public synchronized void log() {
System.out.println("Balance is now " + balance);
}
public static void main(String[] args) {
Account account = new Account();
account.deposit(50);
}
}When deposit runs, the thread holds the lock on account. Then it calls log, which needs the same lock. If locks were not reentrant, the thread would wait for itself to release the lock. It would wait forever. Because the lock is reentrant, the call goes ahead and the program prints Balance is now 50.
How the Lock Counts
The monitor stores two things: the owner (the thread holding it) and a hold count.
- Each acquire by the owner adds 1 to the count.
- Each exit from a synchronized section subtracts 1.
- When the count reaches 0, the lock is free.
Common Mistake
Locking different objects does not protect shared data. If one method locks this and another locks a separate object, two threads can run both methods at the same time. To protect a piece of state, every piece of code that touches it must use the same lock.
Exercise
A thread is inside a synchronized method a() on object obj. Method a() calls synchronized method b() on the same obj, and b() calls synchronized method c() on obj. What is the hold count while c() runs, and when can another thread acquire the lock?
Show answer
The hold count is 3, one for each of a(), b(), and c(). Another thread can acquire the lock only after all three methods have returned and the count is back to 0.
Exercise
Why would the Account example freeze forever if Java's intrinsic locks were not reentrant?
Show answer
deposit already holds the lock on the object. When it calls log, log needs that same lock. Without reentrancy, the thread would block while waiting for a lock it holds itself, and it could never release that lock.