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?
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
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
posted
0
To James
yes i am sure about the output.
To Ruben
even though i put some print statement i am getting the same output.
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
posted
0
ok...
Thanks
Vinay.
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
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.
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
posted
0
Any ideas why this is happening, Punit? I thought notify() notified a single thread.
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
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