Hello, I have a question. In the following code:
class ThreadC extends Thread{
public static void main(
String [] args) {
new ThreadC().metodo1();
}
public synchronized void metodo1()
{
ThreadC b = new ThreadC();
b.start();
// synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {System.out.println("Error al hacer b.wait()");}
System.out.println("metodo1 terminado");
// }
}
public synchronized void run( ) {
// synchronized(this) {
System.out.println("run into ThreadB");
notify( );
System.out.println("Ya se hizo notify()");
try{
Thread.sleep(2000);
}
catch(InterruptedException e){}
System.out.println("ya se libera el lock de ThreadB");
}
// }
}
At runtime: metodo1 generates IllegalMonitorstateException, as far as I now this is because wait() must be called in a synchronized context.
1. Why isn't enough to make metodo1() synchronized?
2. Why notify() works well in run() method just by making it synchronized?