• 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

notify() seems to be notifying all the threads but not just one

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above I am getting the output as

1000
1000
1000

but expecting output to be 1000 and program execution should never finish , because two Reader threads should not be woken up by notify() . Can someone please explain how notify() notified all the three Reader threads ?

thanks.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

program execution should never finish


Why do you believe this to be true?
 
Ravi Tej Pidatala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If two Reader threads are waiting for notify() call from Calculator thread, they would be in waiting state. So either program execution should not finish , or interruptedException should be thrown ?
 
Ravi Tej Pidatala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I comment out the code of line 19 , I am seeing that i am getting same output. It means probably JVM is calling notify() on these threads.
 
Ravi Tej Pidatala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please run the below program and see that the the execution never finishes .

 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravi

I'm not sure what point you are trying to make with your last code snippet. It is obvious that it will never complete as the main thread is set to wait until another thread has a chance to run. There is no other thread so its wait is indefinite.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Tej Pidatala wrote:If two Reader threads are waiting for notify() call from Calculator thread, they would be in waiting state. So either program execution should not finish , or interruptedException should be thrown ?



Ravi Tej Pidatala wrote:When I comment out the code of line 19 , I am seeing that i am getting same output. It means probably JVM is calling notify() on these threads.



What you are seeing is an implementation detail of the threading system -- the threading system uses the threading object to implement join(). The join() method will call wait() on the thread object while the thread is alive -- and part of the shutdown for a thread is to do a notify all on the thread object.

Basically, try not to use the thread object for wait() -- as it is already in use internally.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic