title: Running wait() on a Thread instance from within main() in Java
body: <p>I am playing around with the timed version of wait() in java.lang.Object and have observed that it acts differently in two different scenarios.</p>
  
  <p><strong>Scenario1: Using the default definition of run() in Thread</strong></p>
  
  <pre><code>public static void main (String[] args) throws InterruptedException {
      Thread t = new Thread();    
      t.start();
      System.out.print("X");
      synchronized(t) { t.wait(10000);}
      System.out.print("Y");
  }
  </code></pre>
  
  <p><strong>Questions on scenario1:</strong> I am experiencing a delay between X and Y. Is this because I am calling wait() from main (even though, on t) and therefore the call stack of the main thread is being used, rather than that of the second thread? </p>
  
  <p><strong>Scenario2:</strong> <strong>Subclassing Thread on-the-fly to override run() in order to print something.</strong></p>
  
  <pre><code>public static void main (String[] args) throws InterruptedException {
       Thread t = new Thread() {public void run() 
                       {System.out.print("I am the second thread.");}};
       t.start();
       System.out.print("X");
       synchronized(t) { t.wait(10000);}
       System.out.print("Y");
  }
  </code></pre>
  
  <p><strong>Questions on scenario2:</strong> I am NOT experiencing any delay at all! What has changed just because I have overridden run()? Now, each time I run the program it immediately prints "XI am the second thread.Y" without any delay, whatsoever! Where has the effect of wait() gone?</p>