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!!!!