Help coderanch get a
new server
by contributing to the fundraiser
  • 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

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: 23956
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
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic