• Post Reply Bookmark Topic Watch Topic
  • New Topic

wait() and notify()  RSS feed

 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • 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
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!