public class Provawait
{
public static void main(String args[])
{
Provawait oggetto=new Provawait();
oggetto.go();
}
public void go()
{
System.out.println("Sto per eseguire la wait()");
try
{
wait();
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
System.out.println("qualcuno ha appena eseguito una notify() !!!");
}
}
ma in esecuzione viene generata al seguente eccezione:
Exception in thread "main" java.lang.IllegalMonitorStateException:
current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:474)
at Provawait.go(Provawait.java:14)
at Provawait.main(Provawait.java:6)
Come è possibile ?
Come devo fare per far eseguire correttamente la wait() ?
Grazie a tutte le persone che mi risponderanno
Ciao
Ciao,
questo è quello che dice la documentazione circa l' eccezione che viene
sollevata:
"Thrown to indicate that a thread has attempted to wait on an object's
monitor or to notify other threads waiting on an object's monitor
without owning the specified monitor"
Ti consiglio di dare un' occhiata al manuale della wait(), ti spiega
come utilizzarla al meglio (l' errore in questo caso è che non
hai il lock sull' oggetto).
Ciao,
Andrea.