• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Wait and notify sample does not work

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following wait and notify sample does not work.
Please can somebody let me know what is wrong ?




The output that I see is -
"Notified all the waiting threads"

and then it hangs for ever
[ August 03, 2004: Message edited by: Celina Paul ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This program assumes that waitFor(0) will be called before notifyTo(0), but there's nothing at all that guarantees this will be the case. As written, the two methods could be called in either order, because there's no guarantee which of the two threads will start running first.

If notifyTo() is called first, then the program will hang, as the waitFor() will never return.
 
Celina Paul
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest,

Thanks for the reply.

After reading your comment, I changed the code, so that the Thread t2
sleeps for 10 seconds before calling notify().



This should take care that wait() gets called since the other thread is not sleeping. I still see the same result.

I am expecting to see after the wait is called -
here after being notified

Let me know what is going on here...
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, well, now that you mention it, there is another problem: each Thread has its own TestWaitNotify object, and each TestWaitNotify object has its own array of five Objects -- so that the Object that wait() is called on is never notified; it's another object altogether that is notified, but nothing is waiting.

To fix just use one single TestWaitNotify object to pass to both Thread constructors.
[ August 03, 2004: Message edited by: Ernest Friedman-Hill ]
 
Celina Paul
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great ! It works !

Thanks Ernest,
It also cleared few things on the way...
 
Nothing? Or something? Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic