This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait and notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait and notify" Watch "wait and notify" New topic
Author

wait and notify

Rishi Wright
Ranch Hand

Joined: Jun 25, 2003
Posts: 46
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
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Jun 25, 2003
Posts: 46
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
Todd Killingsworth
Greenhorn

Joined: Jan 30, 2002
Posts: 28
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
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Jun 25, 2003
Posts: 46
well Marlene ..I got the idea from the previous dicussions..Thnx for the link..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait and notify
 
Similar Threads
notify() vs notifyall()
Threads - Notify question
notify() effects on both threads that have/haven't called wait()
Thread
notifyAll() not working