cant explain exception in waitForSignal clas...
Hello to all. When creating this wait-for-signal class (below) I expected no exception, since it remains in the wait state until notified by another Thread. So there should be no output... However the following code:
publicclass Test{
void waitForSignal()throws Exception{
Object obj =new Object();
synchronized(Thread.currentThread()){
obj.wait();
obj.notify();
}
}
publicstaticvoid main(String[] args)throws Exception{
Test tst =new Test();
tst.waitForSignal();
}
}
Throws this exception: Exception in thread"main" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at Test.waitForSignal(Test.java:5)
at Test.main(Test.java:11)
At the moment I am at a loss how to explain this. Any help is appreciated. Thanks.
[1508 byte] By [
DeChristoa] at [2007-11-26 20:23:35]

This is what the API says:IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.is that of any help?
> This is what the API says:
>
> IllegalMonitorStateException - if the current thread
> is not the owner of the object's monitor.
>
> is that of any help?
Thanks for replying. Well, since its the only Thread running, shouldnt it be the owner? The truth is that I am still confused.
> > This is what the API says:
> >
> > IllegalMonitorStateException - if the current
> thread
> > is not the owner of the object's monitor.
> >
> > is that of any help?
>
> Thanks for replying. Well, since its the only Thread
> running, shouldnt it be the owner?
You own an object's monitor by synchronizing on it:
synchronized (foo) { // monitor for the object pointed to by foo acquired here
doStuff();
foo.wait(); // foo's monitor released here
// monitor has been reacquired by the time we get here
moreStuff();
} // monitor released here
http://java.sun.com/docs/books/tutorial/essential/concurrency/
Thanks.. so in the line: synchronized(Thread.currentThread()) I synchronize (or acquire the monitor) for the current Thread right? that means I can call wait() and notify() within the synchronized context... right?
> Thanks.. so in the line:
> synchronized(Thread.currentThread())
I
> synchronize (or acquire the monitor) for the current
> Thread right?
Synchronize a block of code on that Thread, but yes.
> that means I can call wait() and
> notify() within the synchronized context... right?
That means you can call Thread.currentThread().wait()/notify()/notifyAll().
However, is there a reason you want to synchronize on the Thread rather than on something else? Usually some other object is used as the monitor.
> However, is there a reason you want to synchronize on
> the Thread rather than on something else? Usually
> some other object is used as the monitor.
Well, what I had in mind for that class, was to wait for a signal from the current Thread executing, whichever that current thread might be...So I guess the exceptions happens because I try to synchronize on something too general instead of a specific object? .. Thanks.
You have to be synchronized on the object you want to wait on.
ejpa at 2007-7-10 0:49:42 >

> That means you can call
> Thread.currentThread().wait()/notify()/notifyAll().
>
> However, is there a reason you want to synchronize on
> the Thread rather than on something else? Usually
> some other object is used as the monitor.
Note that if the code in the other thread is using the same strategy of synchronizing on the current thread, then the two threads will be using different objects as the monitor. (If you don't see why, it's because "the current thread" means different things in different threads.) That's why synchronizing on the current thread isn't a good idea.