• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Threads

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code:


The output, I think, should be "Trying to Notify" and "Notified" with no certainity of the order in which they will appear.

However, the output is only "Trying to Notify".
Please explain why so?

Thanks.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guptajee,

Change your main with this code: An tell what you got!


waiting...
synchronized("Guptajee") {
try {
wait("Guptajee");
}catch(InterruptedException ie) {
ie.printlnStacktrace();
System.exit(1);
}
}
Regards,
cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
 
author
Posts: 23928
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

However, the output is only "Trying to Notify".
Please explain why so?



Calling the start() method, will create a new thread that will eventually call the run() method. But there is no guarantee that it will call the run() method right away.

In your case, if the main thread returns from the start() method, and calls the restart() method, before the run() method runs, then the notifyAll() will be called before the newly created thread calls wait(). The notification sent by the main thread is lost.

Henry
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic