Seque o código:
public class Waiting implements Runnable {
boolean flag = false;
public static void main(String[] args) {
Waiting w = new Waiting();
new Thread(w).start();
new Thread(w).start();
}
public void run() {
if (flag) {
flag = false;
System.out.print("1 ");
try { wait();
} catch (Exception e) { }
System.out.print("2 ");
} else {
flag = true;
System.out.print("3 ");
try { Thread.sleep(2000);
} catch (Exception e) { }
System.out.print("4 ");
notify();
}
}
}
Por que ao chamar o método notify() lança esta exceção.
<blockquote>3 1 2 4 Exception in thread “Thread-1” java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at capitulo09.definindoInstanciandoIniciando.Waiting.run(Waiting.java:29)
at java.lang.Thread.run(Unknown Source)</blockquote>
Alguém poderia me explicar ?
