• 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 without notify

 
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code prints 1000 and I am not sure why as there is no call to notify anywhere in the code.
I thought that a call to wait would cause the currently running thread to wait until something calls notify on the job object. Since that never happens I assumed the program would just be stuck. Please help clear my thoughts here.

 
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait() doesn't tell the the Thread you're calling it on to wait (indeed, wait() is a method in Object, not in Thread). wait() tells the *current* thread executing it to wait.

Since your job runs separately from the thread calling wait(), the job continues without a problem. Your main thread however would stall forever if it wasn't for spurious wake-ups.
 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, in the example above ...
Is the "current thread" the main thread or the job thread ?
If it is the main thread then how does it move past job.wait() without the notify() being called on the job object somewhere. Is it because of the spurious wake-ups that you talk about ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly.

You should always wrap a call to wait() in a while loop, to check whether the condition that you are waiting for is indeed true.

A simple example:
 
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

This issue comes up often on JR -- and is basically caused by an implementation detail of the join() method. Please do a search for something like "wait join wakeup". For example, this is one of the first hits.

https://coderanch.com/t/268246/java-programmer-SCJP/certification/Thread-wait-notify

Henry
 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for the explanation. I got it now
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic