wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Understanding wait() and notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Understanding wait() and notify() " Watch "Understanding wait() and notify() " New topic
Author

Understanding wait() and notify()

Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
Hi,
I was trying to code a simple example to understand the concept of wait and notify. I got stuck when I tried to code it.
There is a Glass Object which could be emptied and refilled.
There are two persons(Threads) doing it - one the emptying and the other refilling. They have to wait and notify each other if the desired state of the glass is not yet reached.
class Glass{
boolean state; // true means full and false means empty
synchronized public void Drink(){
if (state!=true){ // if glass is empty
notify();
wait();
state=false;
}
}

synchronized public void Refill(){
if (state!=false){ //if glass is already full
notify();
wait();
state=true;
}
}
}
class A implements Runnable{ // Person who drinks
public void run() {
While (true){
// Invoke Drink() method of Glass here
}
}
}
class B implements Runnable{ // Person who refills
public void run(){
while(true){
//Invoke Refill() method of Glass here
}
}
}
I am not able to understand how and where shud I create the glass object so that both the threads A and B have the same Glass object as target.
Cud somebody help me complete the code or correct me if I am wrong in the concept.
Thanks,
Jennifer
Rowan Brownlee
Ranch Hand

Joined: Aug 07, 2000
Posts: 97
Hi Jennifer,
I had a couple of thoughts.
On making sure your drinker and filler are using the same glass object.
~~~~~~~~~~~~~~~~`
One way is to instantiate your Glass object before you instantiate your drinker and filler objects. Then, when you instantiate your drinker and filler, make sure their constructor can receive a reference to your previously instantiated Glass object. That way, they'll both be trying to access the same Glass object.
As for the relationship between drink and fill, I've been known to become confused on this question, but I think that the following logic is correct.

drink() and refill()
~~~~~~~~~~~~~~~~~~~~
If you use a while loop rather than an if statement, the glass will be constantly aware of it's state, and better able to inform all waiting threads through notifyAll() as soon as it's state changes.
I'd put the wait() statement within the while loop (inside a try/catch block for interruptedException catching), and a notifyAll() statement outside the loop.
eg. while loop in the drink method
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while the glass is empty
......tell the requesting thread to wait()
Following while (outside the while loop),
......notifyAll()
......change the state to reflect an empty glass

eg. while loop in the refill method
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while the glass is full
......tell the requesting thread to wait()
Following while (outside the while loop),
......notifyAll()
......change the state to reflect a full glass

That way, when the condition in while changes, control will fall through the while loop, and the notifyAll statement will execute. Any waiting threads will then get a chance to again try to gain access to whichever method they wanted.
cheerio
rowan
Originally posted by Jennifer Wallace:
[B]Hi,
I was trying to code a simple example to understand the concept of wait and notify. I got stuck when I tried to code it.
There is a Glass Object which could be emptied and refilled.
There are two persons(Threads) doing it - one the emptying and the other refilling. They have to wait and notify each other if the desired state of the glass is not yet reached.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Because I need to practise I did the work:


SCJP2. Please Indent your code using UBB Code
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
Thanks Rowan. I was able to complete the coding.
Thanks Bose!
 
Consider Paul's rocket mass heater.
 
subject: Understanding wait() and notify()
 
Similar Threads
wait() and notify()
Section 7 mock test questions
Multithreading: Are two threads performing the same job?
Thread Question from Examlab
Threads Notes for Exams