• 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

Need practical example of wait() and notify()

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been looking up information about threads and I am a little confused about wait() and notify(). Actually, I am trying to think of a practical situation where you would want to use these function calls.
Can anyone help?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you go Kay

i have put the sleep in the main() just to let you see what's going on.
After the program has run, you can see the sequence of events happening, how the first two threads are waiting while the third one has completes and lets the other threads run.
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another simpler example for wait() notify().
code:
**********************************************
// Without using Synchronization, with priority.
class Stwait implements Runnable
{
String s1="null",s2;
int flag=2;
Stwait()
{
Thread t2=new Thread(this,"two");
t2.start();
t2.setPriority(8);
Thread t3=new Thread(this,"three");
t3.start();
t3.setPriority(6);
}
public void run()
{
synchronized(this)
{
for (int i=0;i<3;i++)
{
s2=Thread.currentThread().getName();

if(s2.equals("one"))
{if (flag==1)
{ flag=1;
System.out.println(s2 +" Waiting ");
s1=s2;
try{wait();}catch(Exception e){}
}
System.out.print("firtd");
System.out.print(" ");
flag=1;
System.out.println(s2 +" Notifying: "+s1);
notify();
try{wait();s1=s2;Thread.currentThread().sleep(800);} catch(Exception e){}
}
if(s2.equals("two"))
{ if(flag==2)
{ flag=2;
System.out.println(s2 +" Waiting ");
s1=s2;
try{wait();}catch(Exception e){}
}
System.out.print("sectd");
System.out.print(" ");
flag=2;
System.out.println(s2 +" Notifying: "+s1);
notify();
try{wait();s1=s2;Thread.currentThread().sleep(800);} catch(Exception e){}
}
if(s2.equals("three"))
{ if (flag==3)
{ flag=3;
System.out.println(s1 +" Waiting ");
s1=s2;
try{wait();}catch(Exception e){}
}
System.out.print("thrtd");
System.out.print(" ");
flag=3;
System.out.println(s2 +" Notifying: "+ s1);
notify();
try{wait();s1=s2;Thread.currentThread().sleep(800);} catch(Exception e){}
}
}
}
}
public static void main(String ars[])
{
Thread.currentThread().setPriority(10);
Thread t1=new Thread(new Stwait(),"one");
t1.start();
t1.setPriority(4);
System.out.print(" out of main");
}
}
*****************************************************
end of code:
enjoy middling with java
Netharam
[This message has been edited by netharam ram (edited November 21, 2001).]
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic