Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait( ) and notify( )

 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this out :
http://java.sun.com/docs/books/tutorial/essential/threads/waitAndNotify.html
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant 'CANT' BE THAT SIMPLE !!!
 
Anna Dow
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for misspelling ur name "Francisco"
Deepali
 
Pavan Srivastava
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic