• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait() and notify()

 
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class waitnotify implements Runnable
{
public boolean i;

public void run()
{
}

public synchronized void setBool()
{
if(i== false)
{
try{
wait(500);
}catch(Exception e){}
}


}
public static void main(String []arg)
{
a w = new a();
a A = new a();
Thread t = new Thread(w);
Thread t1 = new Thread(A);
//t.setDaemon(true);
//t1.setDaemon(true);
t.start();

t1.start();
}
}
class a extends waitnotify
{
public void run()
{
setBool();
setBooltr();

}
public synchronized void setBooltr()
{
i = true;
System.out.println("notified");
notify();

}
}
please tell how to implemnt wait () and notify() in synchronized code . I have got the concept but unable to implement . The following code calls the wait() method but is not getting the notification . please correct it and explain the concept with actual implementation .
thanks a ton
regards
raghav mathur
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you go
This class creates two threads objects from the Ping and Pong class, each thread output's the string "Ping" and "Pong" respectivly.
The code uses wait and notify to get the two thread to work together to output the string, "PingPong"


[ March 02, 2002: Message edited by: Rajinder Yadav ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic