File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between notify and notifyAll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between notify and notifyAll" Watch "Difference between notify and notifyAll" New topic
Author

Difference between notify and notifyAll

Vinu Menon
Greenhorn

Joined: Jan 26, 2009
Posts: 20
if i am not wrong ,if many threads act on an object ,using "notify()" will affect only one of them,where as "notifyAll()" will notify all of the threads that are waiting on a particular object.
Consider the below example



when i use notifyAll i get the following output:
D:\scjp>javac Reader.java

D:\scjp>java Reader
A waiting for calculation....
B waiting for calculation....
C waiting for calculation....
going inside Calculators syn block
inside Sync block
Total of Thread Cis4950
Total of Thread Bis4950
Total of Thread Ais4950

when i use notify i get the following output:
D:\scjp>javac Reader.java

D:\scjp>java Reader
A waiting for calculation....
C waiting for calculation....
B waiting for calculation....
going inside Calculators syn block
inside Sync block
Total of Thread Ais4950
Total of Thread Bis4950
Total of Thread Cis4950

Now my question is:"notify()" should notify only one of the thread how is that all the threads is getting notified?
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

EDIT: does not make sense...


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Vinu Menon
Greenhorn

Joined: Jan 26, 2009
Posts: 20
but a thread in wait state will come into runnable state only when a notify is signaled.so how is that the other two threads run?
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Edited my previous post... answered without reading the complete question...
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580


when i use notify i get the following output:
D:\scjp>javac Reader.java

D:\scjp>java Reader

A waiting for calculation....
C waiting for calculation....
B waiting for calculation....
going inside Calculators syn block
inside Sync block
Total of Thread Ais4950
Total of Thread Bis4950
Total of Thread Cis4950


Are you sure you got that output...
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
To troubleshoot this mystery, try to print something inside the catch block corresponding to the try where you are waiting. I think the threads are receiving an InterruptedException and that's why they are waking up. The JVM sometimes will wake threads in a wait state "for no reason."


All code in my posts, unless a source is explicitly mentioned, is my own.
Vinu Menon
Greenhorn

Joined: Jan 26, 2009
Posts: 20
To James
yes i am sure about the output.

To Ruben
even though i put some print statement i am getting the same output.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Hope you have K&B 6 book .In page 755 , the book tells the reason for this behaviour as told by Ruben...
Here is part of that paragraph..
K&B wrote:it's possiblethat a thread has accidentally sent an extra notify() that was not intended.
There's also a possible situation called spontaneous wakeup that may exist in some
situations—a thread may wake up even though no code has called notify()
or notifyAll(). (At least, no code you know about has called these methods.
Sometimes the JVM may call notify() for reasons of its own, or code in some other
class calls it for reasons you just don't know.)
Vinu Menon
Greenhorn

Joined: Jan 26, 2009
Posts: 20
ok...
Thanks
Vinay.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Actually, this is not what is going on here.

I ran this code:

And this was the output:
A waiting for calculation....
B waiting for calculation....
going inside Calculators syn block
inside Sync block
C waiting for calculation....
Total of Thread Bis4950
Total of Thread Ais4950
// Program doesn't return

It is strange that both Thread A and Thread B seem to be notified, as only one of them should be notified.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

but when running the program few more times....
sometimes only one is notified,sometimes two are notified,and sometimes all threee are notified.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I also ran the code and got the same output as Vinu Menon got.


SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Any ideas why this is happening, Punit? I thought notify() notified a single thread.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I think the reason is what James has given
Hope you have K&B 6 book .In page 755 , the book tells the reason for this behaviour as told by Ruben...
Here is part of that paragraph..
K&B wrote:
it's possiblethat a thread has accidentally sent an extra notify() that was not intended.
There's also a possible situation called spontaneous wakeup that may exist in some
situations—a thread may wake up even though no code has called notify()
or notifyAll(). (At least, no code you know about has called these methods.
Sometimes the JVM may call notify() for reasons of its own, or code in some other
class calls it for reasons you just don't know.)


K&B has clearly written about JVM could notify itself sometimes, I read this page in the book and it is saying always use wait() in while loop with some condition checking as notify() could be called by JVM for reasons of its own.
The moral here is that when you use wait() and notify() or notifyAll(), you should almost always also have a while loop around the wait() that checks a condition and forces continued waiting until the condition is met



Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You are right. I was only focusing on the InterruptedException.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between notify and notifyAll