Home > coding, java > How does Java’s Object.wait() really work?

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:

  1. In Object.java wait() calls the native method wait(timeout) via wait(0)
  2. The native code for wait in Object.c actually uses JVM_MonitorWait.
  3. The JVM wrapper method JVM_MonitorWait calls ObjectSynchronizer::wait.
  4. ObjectSynchronizer::wait calls wait() on an ObjectMonitor.
  5. 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)

Advertisements
Categories: coding, java Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: