• 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
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 23959
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
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic