File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes notify() seems to be notifying all the threads but not just one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "notify() seems to be notifying all the threads but not just one " Watch "notify() seems to be notifying all the threads but not just one " New topic
Author

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

Ravi Tej Pidatala
Greenhorn

Joined: Jan 29, 2012
Posts: 14


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

Joined: Nov 09, 2011
Posts: 1012
    
    5

program execution should never finish

Why do you believe this to be true?
Ravi Tej Pidatala
Greenhorn

Joined: Jan 29, 2012
Posts: 14
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

Joined: Jan 29, 2012
Posts: 14
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

Joined: Jan 29, 2012
Posts: 14
Please run the below program and see that the the execution never finishes .

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

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
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notify() seems to be notifying all the threads but not just one