This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ravi Tej Pidatala
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
program execution should never finish

Why do you believe this to be true?
 
Ravi Tej Pidatala
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please run the below program and see that the the execution never finishes .

 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 2
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic