• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Thread/notify/notifyAll/wait question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!
I was studying for the exam and was surprised that the following problem was not in the SCJP book:


As this code demonstrates the run method of ThreadTest will be waiting for ever. How is this problem resolved? Is there another way than using the timer in the wait statement?

--Tomas
[ May 20, 2007: Message edited by: Tomas Jansson ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible that the notifyAll() is being invoked before the wait() is being invoked and thereby missing those notifications.


I modified the code slightly by adding a sleep() before notifyAll()


This prints:

First some sleep...
First some sleep...
First some sleep...
I am notifiying everyone
Waiting for calculation...
Waiting for calculation...
Waiting for calculation...
Total is: 49995000
Total is: 49995000
Total is: 49995000
[ May 20, 2007: Message edited by: M Krishnan ]
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would add a method to calculator indicating, that calculation is still running, and check for that before calling wait(). (So I can be sure, that someone will notify me)
 
Something about .... going for a swim. With this tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic