0

First code:

public class H extends Thread {

String info = "";

public H (String info) {
    this.info    = info;
}
public synchronized void run() {
   try {

    while ( true )  {
        System.out.println(info);
        notify();
        wait();
    }
   } catch ( Exception e )  {}
}
public static void main (String args []) {
    new H("0").start();
    new H("1").start();
}

}

Second code:

public class H extends Thread {

String info = "";

static Object o = new Object();
public H (String info) {
    this.info    = info;
}
public synchronized void run() {
   try {

    while ( true )  {
        System.out.println(info);
        o.notify();
        o.wait();
    }
   } catch ( Exception e )  {}
}
public static void main (String args []) {
    new H("0").start();
    new H("1").start();
}

}

If first code I agree it will go into deadlock state as synchronized run method will never release lock.

But why in second code if the whole run method is synchronized with no object sill I am trying to wait and notify object o, it should either go in deadlock or run the code continuously(because of while (true)) , but the code exists with exit code 0. Can anyone help me on this. Thanks in advance!!!!

Yash Jain
  • 51
  • 1
  • 6
  • In the first code there isn't any thread holding the monitors lock that will wake the waiting thread. In the second one the missing printStackTrace() hides this error: java.lang.IllegalMonitorStateException See the API doc for the wait and nofity methods in the Object class. – NormR Oct 29 '15 at 00:58

1 Answers1

1

The second example of code you have is not doing what you think it is.

You're just catching Exception and burying it, without any info. When o.notify() is called, it will throw an IllegalMonitorStateException, therefore breaking out of the while loops and causing your program to exit with return code 0.

In order to properly call notify, the currently executing thread must have a lock on the object you are notifying.

In example 1, the synchronized run() method is holding a lock on this, so you're able to do this.notify().

However, in the second example, there is no synchronization on object o when you notify it, thus causing theIllegalMonitorStateException.

Here is a "fixed" copy of the second version of code. This will loop infinitely alternating between printing 0's and 1's, which I think is what you were expecting:

public class H extends Thread{
    String info = "";

    static Object o = new Object();
    public H (String info) {
        this.info    = info;
    }

    public synchronized void run() {
       try {

        while ( true )  {
            System.out.println(info);
            synchronized(o){
                o.notify();
                o.wait();
            }
        }
       } catch ( Exception e )  {
           throw new RuntimeException(e);
       }
    }
    public static void main (String args []) {
        new H("0").start();
        new H("1").start();
    }
}
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61