aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait( ) and notify( ) 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 "wait( ) and notify( )" Watch "wait( ) and notify( )" New topic
Author

wait( ) and notify( )

Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Can someone give me a very simple working code to see wait and notify working on 2 objects.
I have got the theory behind it but when i try to write a code, it never behaves well.
Thnx
Deepali
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Check this out :
http://java.sun.com/docs/books/tutorial/essential/threads/waitAndNotify.html


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
I have that document printed and actually that is the only simple code i saw with wait() and notify(). But i wanted to make it more simple and so i amde this.
[/code]
class Shop
{
synchronized void put()
{
for(int i=0;i<5;i++)
{
System.out.println("Method1............"+i);
try
{
wait();
}
catch(InterruptedException e){}
System.out.println("Method1 after wait............"+i);
}
notify();}
synchronized void get()
{
for(int i=5;i<10;i++)
{
System.out.println("Method2............"+i);
try
{
wait();
}
catch(InterruptedException e){}
System.out.println("Method2 after wait............"+i);
}notify();}}

class Producer extends Thread
{
private Shop s1;
Producer(Shop s){s1=s;}
public void run()
{
s1.put();
}
}
class Consumer extends Thread
{
private Shop s1;
Consumer(Shop s){s1=s;}
public void run()
{
s1.get();
}
}
public class mainwait
{
public static void main (String args[]) throws InterruptedException
{
Shop s=new Shop();
Producer p=new Producer(s);
Consumer c=new Consumer(s);
p.start();
c.start();
}
}
[code]
It compiles and runs but both call wait() and it gets stuck.
Can u help me?? Or is it just that wait() and notify() can be that simple.
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
I meant 'CANT' BE THAT SIMPLE !!!
Anna Dow
Greenhorn

Joined: Jun 04, 2002
Posts: 18
Hi, Deepali.
I think it's no surprise that the program is stuck. The first thread won't get out of waiting until the second one will call notify(). However, it will never happen, because the second thread is waiting to be notified itself (by the first thread). I think it will work if you move notify() in front of wait():

Sorry, cannot verify it myself - has not got any java environment on my home machine.
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
Deepali, I changed your program a bit:

Now, let�s say that your shop can�t have more than 5 products on the shelf. So, the put() method checks if the shop has 5 products. If it has, it waits untill someone consumes one of those products.If the shop has less than 5 products, the method increase the number of products, and notifies the system it has produced an product.
On the other hand, you can�t consume if there are no products, so the get() method checks if the shop has 0 products.If so, it waits untill someone produces a product. If the shop has more than 0 products, it decreases the number of products and notifies the system it has consumed a product.
I hope this helped, if you have any more doubts, please let me know.
Francisco


Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Thanx a lot to Anna and Fransico,
Both ur suggesstion worked.
I have thought that if i call notify() before wait() i would get an error coz i had encountered something like that before.
But now things are much clear to me thnx
Deepali
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Sorry for misspelling ur name "Francisco"
Deepali
Pavan Srivastava
Greenhorn

Joined: Jul 30, 2002
Posts: 3
Hi Deepali,
I think you should go though these links. They are really good to get the grip of the Threads.
Just make sure that you go sequentially and read all the 3 links.
Regards,
-p
http://www.javaworld.com/javaworld/jw-05-2002/jw-0503-java101_p.html
http://www.javaworld.com/javaworld/jw-06-2002/jw-0607-java101.html?
http://www.javaworld.com/javaworld/jw-07-2002/jw-0703-java101.html?


Success is all about overriding your defaults.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait( ) and notify( )