synchronized, monitors, wait and notify question 27 of 180 mid
What is reentrancy, and what would break without it?
- Reentrancy means two threads can enter the same block
- Correct: A thread already holding a monitor can acquire it again; without it, any synchronized method calling another would self-deadlock
- It allows recursion only up to the stack depth
- It applies to
ReentrantLockbut not tosynchronized
A monitor records its owner and a hold count. The owning thread entering again just increments the count, and the monitor is released when the count reaches zero.
Without reentrancy, an ordinary pattern would hang: a synchronized method calling another synchronized
method on the same object, or a synchronized equals on a subclass calling super.equals. Both
synchronized and ReentrantLock are reentrant, which is what the name says. ReadWriteLock is the
sharp edge here: a read lock cannot be upgraded to a write lock, and trying deadlocks the thread
against itself.
Quiz yourself on synchronized, monitors, wait and notify Suggest a correction