wait() and notify() are not static, so the compiler should give error that wait must be called from static context.
public class Rolls {
public static void main(String args[]) {
synchronized(args) {
try {
wait();
} catch(InterruptedException e)
{ System.out.println(e); }
}
}
}
But the following code compiles and runs properly. Why does compiler not give errors here? Alternatively, why does the compiler give the error in the previous code that wait must be called from a static context?
public class World implements Runnable {
public synchronized void run() {
if(Thread.currentThread().getName().equals("F")) {
try {
System.out.println("waiting");
wait();
System.out.println("done");
} catch(InterruptedException e)
{ System.out.println(e); }
}
else {
System.out.println("other");
notify();
System.out.println("notified");
}
}
public static void main(String []args){
System.out.println("Hello World");
World w = new World();
Thread t1 = new Thread(w, "F");
Thread t2 = new Thread(w);
t1.start();
t2.start();
}
}