• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between notify and notifyAll

 
Vinu Menon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDIT: does not make sense...
 
Vinu Menon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edited my previous post... answered without reading the complete question...
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
Vinu Menon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...
Thanks
Vinay.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also ran the code and got the same output as Vinu Menon got.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas why this is happening, Punit? I thought notify() notified a single thread.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I was only focusing on the InterruptedException.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic