title: JDK implementation of Thread.join()
body: <p>I was wondering how Java implements the join() method to wait for a thread to finish. According to the <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Thread.java#Thread.join%28long%29">source code</a>:</p>
  
  <pre><code>public final synchronized void  [More ...] join(long millis)
  throws InterruptedException {
      long base = System.currentTimeMillis();
      long now = 0;
  
      if (millis &lt; 0) {
          throw new IllegalArgumentException("timeout value is negative");
      }
  
      if (millis == 0) {
          while (isAlive()) {
              wait(0);
          }
  
      } else {
          while (isAlive()) {
              long delay = millis - now;
              if (delay &lt;= 0) {
                  break;
              }
              wait(delay);
              now = System.currentTimeMillis() - base;
          }
      }
  }
  </code></pre>
  
  <p>The calling thread obtains the running thread's monitor and wait() at line 1160 indefinitely while running thread is still alive.</p>
  
  <p>My question is: where is (and who calls) the notify() or notifyAll() when the thread completes, so that it wakes up the calling thread?</p>
  
  <p>To be perfectly clear, the question is about <strong>where in the JDK/JVM</strong> is notify() called, not in our code.</p>
  
  <p>Thank you.</p>