TrainBeaser for iPhone
The moose likes Threads and Synchronization and the fly likes wait() and notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "wait() and notify()" Watch "wait() and notify()" New topic
Author

wait() and notify()

Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 639
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


Raghav.
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
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 ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
 
IntelliJ Java IDE
 
subject: wait() and notify()
 
Threads others viewed
wait without notify
Threads - Notify question
Test Question about Threads
Understanding wait() and notify()
Threads
IntelliJ Java IDE