• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

wait and notify

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I was trying to implement wait and notify methods for an object but I am unable to call notify method.Please help me if Iam missing something here.
here is the code
public class thread implements Runnable{
public static void main(String []args){
thread t1=new thread();
thread t2=new thread();
//Thread mythread1=new Thread(t1,"thread1");
//Thread mythread2=new Thread(t1,"thread2");

t1.wait_mthd();
t1.notify_mthd();
//how can i run notify method???and why above //method call is not releasing lock on t1
}
public synchronized void notify_mthd(){
System.out.println("This is first synchronized notify method ");
this.notify();
System.out.println("This is after notify call");
}
public synchronized void wait_mthd(){
System.out.println("This is synchronized waiting method for "+this);
try{
this.wait();}
catch(InterruptedException e){}
System.out.println("This is waiting method after notified");
}
public void run(){
for(int i=0;i<6;i++){
//this.wait_mthd();
System.out.println("This is first statement after wait"+Thread.currentThread().getName());
}

}
}

thanks in advance
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rishi
There is only one user thread: the main thread in your application. When you invoke wait on that thread there is no one to invoke notify on it. The current code that you have is only a single threaded application. You have to call Thread.start() method to start you thread. You will have to make a couple of changes to make your current application multithreaded.
[ July 22, 2003: Message edited by: Anupam Sinha ]
 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Anupam...I was in the misconception that calling a method will start a thread..I tried the above code starting a thread its working fine...
But how come the wait() and notify() methods defined in Object class if they are not related to the object??
I will be grateful if u could further help...
thnx
rishi
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Object class is parent for any object.
All other classes derive from Object.
Adding wait(), notify() and notifyAll() at this low level in the inheiritance tree allows any object to be used as a concurrency lock.
Putting this support only in class Thread wouldn't be nearly as flexible.
Todd Killingsworth
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a link to a recent discussion on why wait and notify are defined in the Object class. Here
Notice this link also has links.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But how come the wait() and notify() methods defined in Object class if they are not related to the object??


Because a Java object is a monitor in the sense of C.A.R. Hoare (1974) and Brinch Hansen (1975).
A good introduction to monitors is Andrew Tanenbaum, Modern Operating Systems, Chapter 2 Processes and Threads, Section 2.3.7 Monitors
 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well Marlene ..I got the idea from the previous dicussions..Thnx for the link..
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic