GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Monitors wait and notifyAll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Monitors wait and notifyAll" Watch "Monitors wait and notifyAll" New topic
Author

Monitors wait and notifyAll

Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
Hey there,
I've been fighting with these for a couple of days and I hope one of ya'll can help me. I understand that the monitors wait and notifyAll have to be called from synchronized code and probably not the same piece of synchronized code either. But when I use 2 different methods and try to call notifyAll from the second synchronized method, I get illegalMonitorStateException error. Is this because the thread executing notifyAll doesn't have the lock on the waiting thread?
I'm callling the start method so that the 2 threads will run concurrently but for the life of me I can't seem to be able to notify either one. They seem to hang on the wait()
Can anyone help me? I'd introduce some code but it is really simple, try catch blocks with wait and notifyAll inside.
Thx
J
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
It seems that you should post the code because from your description you are doing everything correctly.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
I hope this helps clarify. Thanks again to anyone that can help.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
As far as I know, the only way you can get a
IllegalMonitorStateException error
is like you say, by calling one of the monitor related methods when the Thread is not synchronized on the object.
As far as why your threads never get notified, I can't see where you expect that to happen.
Bill
------------------
author of:
Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
OK so where should I put the notifyAll() ? I tried it in the run method before the call to methodB and got IllegalMonitorStateException. I even created a new synchronized method called Notify and called notifyAll from there with yet a third thread. Same problem. My basic question is how to notify a waiting thread? Maybe I'm missing something but I just can't seem to get it to work at all. Any suggestions??
Thx
J
Jennifer Warren
Ranch Hand

Joined: Aug 24, 2001
Posts: 53
Jay Brass, Hi there,
Thing is that theres got to be a reason for synchronizing the two methods. Usually when two methods are dependent on each other. In your code both threads calls the mehtodA and are passed in to wait state. Try putting notifyAll() before the wait(), the code will work. I hope this helps.
Regards.
Jennifer.
Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
Hey Jennifer,
The only way I could get the results I wanted was to do this:

Which is little different than calling sleep(1000) except that sleep is static. I peppered the code with notifyAll()'s and still couldn't get it out of the wait state. I think because when notifyAll is called the notified thread can't aquire the lock because the other thread has it. So it is forced to wait again. Then the second thread waits and no one ever notifies it. Can someone verify this? And can someone tell me where I can apply a notifyAll command to give me the same results as above?
Thx
J
Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
sorry there should be a
new SimpleThreads("Thread 2").start();
included in the above code. Darn that cut and paste!
J
 
GeeCON Prague 2014
 
subject: Monitors wait and notifyAll