views:

97

answers:

2

Hi, I've got the following code, which I expected to deadlock after printing out "Main: pre-sync". But it looks like synchronized doesn't do what I expect it to. What happens here?

import java.util.*;

public class deadtest {
   public static class waiter implements Runnable {
      Object obj;

      public waiter(Object obj) {
         this.obj = obj;
      }

      public void run() {
         System.err.println("Thead: pre-sync");
         synchronized(obj) {
            System.err.println("Thead: pre-wait");
            try {
               obj.wait();
            } catch (Exception e) {
            }
            System.err.println("Thead: post-wait");
         }
         System.err.println("Thead: post-sync");
      }
   }

   public static void main(String args[]) {
      Object obj = new Object();

      System.err.println("Main: pre-spawn");

      Thread waiterThread = new Thread(new waiter(obj));
      waiterThread.start();

      try {
         Thread.sleep(1000);
      } catch (Exception e) {
      }  

      System.err.println("Main: pre-sync");
      synchronized(obj) {
         System.err.println("Main: pre-notify");
         obj.notify();
         System.err.println("Main: post-notify");
      }
      System.err.println("Main: post-sync");

      try {
         waiterThread.join();
      } catch (Exception e) {
      }
   }
}

Since both threads synchronize on the created object, I expected the threads to actually block each other. Currently, the code happily notifies the other thread, joins and exits.

+3  A: 

When you wait() on an object, the thread releases the lock on the object to allow others to aquire the lock and notify() the waiting thread. See the javadoc for Object.wait().

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

Gerco Dries
+4  A: 

Calling .wait() on a monitor actually releases the synchronized lock so the other thread can lock on to the same monitor and send a notification.

Your behavior is completly normal: "waiter" locks on a monitor and then releases the lock when waiting for notification. After 1 second the main thread locks the monitor, sends notification, unlocks the monitor, which wakes the waiter to complete its operation.

Mavrik