• 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

wait and notify

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic