Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IllegalMonitorStateException

0 views
Skip to first unread message

alessandro.py

unread,
Aug 17, 2006, 1:40:56 PM8/17/06
to
Ciao a tutti stavo provando a usare le operazioni di wait() e notify()
così ho scritto questo codice:

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

Andrea Cerisara

unread,
Aug 18, 2006, 6:53:24 AM8/18/06
to
alessandro.py wrote:
> 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.

alessandro.py

unread,
Aug 18, 2006, 12:56:02 PM8/18/06
to
Ho scoperto il problema :
il metodo da cui si richiama la wait() (o la notify()) DEVE avere il
modificatore synchronized !!!
grazie ancora
ciao

0 new messages