How does Java’s Object.wait() really work?
Recently, I came across a question whether Object.wait() uses an “infinite loop”.
I didn’t know the answer, but since the JDK is Open Source, I thought I could at least find out where the code is and look at it.
So for example for JDK 7, these are the steps through the OpenJDK sources:
- In Object.java wait() calls the native method wait(timeout) via wait(0)
- The native code for wait in Object.c actually uses JVM_MonitorWait.
- The JVM wrapper method JVM_MonitorWait calls ObjectSynchronizer::wait.
- ObjectSynchronizer::wait calls wait() on an ObjectMonitor.
- Finally ObjectMonitor::wait does the real work.
I don’t fully understand that code, but I certainly don’t see an infinite loop in there. :o)