Module: | Arrays, Strings & Exception Handling
Q53: Consider the following statements regarding the sleep() and wait() mechanisms:
1. The sleep() method is a static method belonging to the java.lang.Thread class, whereas the wait() method is an instance method defined within the supreme java.lang.Object class.
2. When a thread executes the sleep() method inside a synchronized block, it temporarily releases the monitor lock to allow other threads to enter the critical section while it sleeps.
3. The wait() method strictly mandates being called from within a synchronized context; otherwise, the JVM will throw an IllegalMonitorStateException at runtime.
Which of the above statements is/are correct?
2. When a thread executes the sleep() method inside a synchronized block, it temporarily releases the monitor lock to allow other threads to enter the critical section while it sleeps.
3. The wait() method strictly mandates being called from within a synchronized context; otherwise, the JVM will throw an IllegalMonitorStateException at runtime.
Which of the above statements is/are correct?
✅ Correct Answer: B
🎯 Quick Answer:
The correct combination is 1 and 3. Statement 2 is incorrect because the sleep() method intentionally retains total ownership of the monitor lock while paused. It does not release the lock, meaning all other threads attempting to enter the synchronized block will remain completely blocked until the sleeping thread wakes up and finishes execution.Structural Breakdown: `Thread.sleep(1000)` pauses the thread for 1 second. `object.wait()` pauses the thread indefinitely until another thread explicitly calls `object.notify()` or `object.notifyAll()`. Historical/Related Context: The wait() method resides in the Object class (Statement 1) because locks in Java are bound to Objects, not to Threads.
Since every object in Java can act as a synchronization monitor, the mechanism to release that specific monitor (wait) must inherently belong to the universal Object blueprint.
Causal Reasoning: The JVM strictly enforces wait() to be inside a synchronized block (Statement 3) to prevent catastrophic "lost wake-up" race conditions.
A thread must mathematically prove it actually owns the lock on an object before it is legally allowed to command that object to release the lock and enter a waiting state.