• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Can a waiting thread be notified without an explicit notify invocation on the synchronized object ?

 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output is :-


laurel: starts running
laurel: going to sleep
hardy: starts running
hardy: going to wait for laurel
laurel: out from sleep
laurel: is exiting run
hardy: IS OUT FROM WAIT ON LAUREL.
hardy: is exiting run



Can any one please explain how the thread 'hardy' came-out from the waiting and who notified on the synchronized object 'laurel'? Please note that there is no laurel.notify() statement anywhere in the code-snippet.

Also if I comment-out the try-catch including the Thread.sleep, hardy waits forever, though laurel completes its execution.

Thanks
Raj
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you wait on a Thread object then you get a notification when the target thread completes. So if Thread A is waiting on Thread B object, then Thread A will be notified when Thread B completes...
 
Rajanand Pandaraparambil Kuttappan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if Thread A is waiting on Thread B object, then Thread A will be notified when Thread B completes...



So, JLS treats this an exceptional case for the thread being used as a monitor, right? Because, theoretically speaking, if there is an object1.wait() from ThreadA, ThreadA would wait until there comes a object1.notify() from another thread which again synchronized on object1. And, in our case, we do not have an explicit notify call at all.

-Raj
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot rely on this behavior. This happens because Thread.join has been implemented this way but JLS doesn't specify this behavior. So this behavior might change in the future thus it will not be asked in SCJP exam ...
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also I think wait(), notify(). notifyAll() are not part of OCP-JP syllabus anymore. Please check.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic