File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait and notify" Watch "wait and notify" New topic
Author

wait and notify

avi aro
Greenhorn

Joined: Apr 13, 2006
Posts: 16
Hi,
Is it possible for a thread to come out of wait state even when the other thread has not called notify. I found the following example in the Sun Certified Programmer and Developer book on thread. This example uses notifyAll(). But even if I comment notify, the program works fine. Any suggestions. how is it possible:


[ May 06, 2006: Message edited by: Mark Spritzler ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

Originally posted by avi aro:
Hi,
Is it possible for a thread to come out of wait state even when the other thread has not called notify. I found the following example in the Sun Certified Programmer and Developer book on thread. This example uses notifyAll(). But even if I comment notify, the program works fine. Any suggestions. how is it possible:


It is an implementation artifact. When the run() method completes, one of the cleanup operations for a thread is to issue a notifyAll() on the thread object. This is needed to wakeup all of the threads that are waiting for the thread to finish -- via the join() method.

In other words, it is working because a notifyAll() *is* being issued.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nilesh Deshpande
Ranch Hand

Joined: Jan 11, 2006
Posts: 36
Even if I change the line notifyAll() to notify(), it gives the sam result. Some one please help me to understand the effect of notify and notifyAll() in this example.

Thanks in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

Originally posted by Nilesh Deshpande:
Even if I change the line notifyAll() to notify(), it gives the sam result. Some one please help me to understand the effect of notify and notifyAll() in this example.

Thanks in advance.


As mentioned, there is an extra notifyAll() that is issued when a thread completes. So by changing the example from using notifyAll() to a notify(), it is going from 2 notifyAll() calls, to a single notify() followed by a notifyAll().

Henry
avi aro
Greenhorn

Joined: Apr 13, 2006
Posts: 16
Hi Henry,
Thanks a lot for your answer. One more thing. Is notifyAll() issued by all the threads when they finish. So if we have a thread that is going to finish, then we don't need a notifyAll() to terminate other threads.
Thanks.
Avi
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

"avi"

Please click on the My Profile link above and change your display name to match the JavaRanch naming policy of using your real first and real last names.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait and notify