Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait and notify

 
avi aro
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nilesh Deshpande
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic